Step by step - Using signals

This commit is contained in:
Laegnur 2024-09-11 19:03:50 +02:00
parent 6240748512
commit bc3ad87870
Signed by: laegnur
GPG key ID: F8FE83A0B2D932A3
2 changed files with 33 additions and 12 deletions

19
scenes/button.tscn Normal file
View file

@ -0,0 +1,19 @@
[gd_scene load_steps=2 format=3 uid="uid://cn4bsud8kgyvp"]
[ext_resource type="PackedScene" uid="uid://6ws803s4tnhy" path="res://scenes/icon.tscn" id="1_or1o1"]
[node name="Node2D" type="Node2D"]
[node name="Icon" parent="." instance=ExtResource("1_or1o1")]
[node name="Timer" type="Timer" parent="Icon"]
autostart = true
[node name="Button" type="Button" parent="."]
offset_left = 48.0
offset_top = 33.0
offset_right = 238.0
offset_bottom = 108.0
text = "Toggle motion"
[connection signal="pressed" from="Button" to="Icon" method="_on_button_pressed"]

View file

@ -3,17 +3,19 @@ extends Sprite2D
var speed = 400
var angular_speed = PI
func _ready():
var timer = get_node("Timer")
timer.timeout.connect(_on_timer_timeout)
func _process(delta: float) -> void:
var direction = 0
if Input.is_action_pressed("ui_left"):
direction = -1
if Input.is_action_pressed("ui_right"):
direction = 1
rotation += angular_speed * direction * delta
var velocity = Vector2.ZERO
if Input.is_action_pressed("ui_up"):
velocity = Vector2.UP.rotated(rotation) * speed
rotation += angular_speed * delta
var velocity = Vector2.UP.rotated(rotation) * speed
position += velocity * delta
func _on_button_pressed() -> void:
set_process(not is_processing())
func _on_timer_timeout():
visible = not visible