From bc3ad87870afae86f3fcc73f89c3057280d2612e Mon Sep 17 00:00:00 2001 From: Laegnur Date: Wed, 11 Sep 2024 19:03:50 +0200 Subject: [PATCH] Step by step - Using signals --- scenes/button.tscn | 19 +++++++++++++++++++ scripts/icon.gd | 26 ++++++++++++++------------ 2 files changed, 33 insertions(+), 12 deletions(-) create mode 100644 scenes/button.tscn diff --git a/scenes/button.tscn b/scenes/button.tscn new file mode 100644 index 0000000..7d29af3 --- /dev/null +++ b/scenes/button.tscn @@ -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"] diff --git a/scripts/icon.gd b/scripts/icon.gd index 91de13e..6c7bd70 100644 --- a/scripts/icon.gd +++ b/scripts/icon.gd @@ -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