Tutorial4

master
Sascha 2023-09-01 17:37:40 +07:00
parent 3b26592bb6
commit 2cfeda9c12
4 changed files with 32 additions and 28 deletions

@ -9,7 +9,9 @@ position = Vector2(460, 266)
texture = ExtResource("1_h0rd0")
script = ExtResource("2_4tdcf")
[node name="Child" type="Sprite2D" parent="."]
[node name="Tomato" type="Sprite2D" parent="."]
position = Vector2(100, 100)
scale = Vector2(0.5, 0.5)
texture = ExtResource("3_ffpuj")
[node name="Clock" type="Timer" parent="."]

@ -3,16 +3,24 @@ using Godot;
namespace Tutorial1.Scripts;
public partial class Abdullah : Sprite2D {
private const int AMOUNT = 5;
private Sprite2D child;
private Timer clock;
public override void _Ready() => child = GetNode<Sprite2D>("Child");
[Signal] public delegate void MovedEventHandler(float x, float y);
public override void _Process(double delta) {
uint randomNumber = GD.Randi() % 4;
if (randomNumber == 0) Position += new Vector2(0, -AMOUNT);
if (randomNumber == 1) Position += new Vector2(0, AMOUNT);
if (randomNumber == 2) Position += new Vector2(-AMOUNT, 0);
if (randomNumber == 3) Position += new Vector2(AMOUNT, 0);
public override void _Ready() {
clock = GetNode<Timer>("Clock");
clock.WaitTime = 1;
clock.Timeout += ClockOnTimeout;
clock.Start();
Moved += OnMoved;
}
private static void OnMoved(float x, float y) => GD.Print($"Moved x={x},y={y}");
private void ClockOnTimeout() {
Vector2 newPosition = new(GD.RandRange(0, 500), GD.RandRange(0, 500));
Position = newPosition;
EmitSignal("Moved", newPosition.X, newPosition.Y);
}
}

@ -3,15 +3,15 @@ using Godot;
namespace Tutorial1.Scripts;
public partial class World : Node2D {
private PackedScene packedScene;
public override void _Ready() => packedScene = GD.Load<PackedScene>("res://Abdullah.tscn");
public override void _UnhandledInput(InputEvent @event) {
if (@event is InputEventMouseButton mouseEvent)
//Create instance of Sprite scene
if (packedScene.Instantiate() is Sprite2D abdullah) {
abdullah.Position = mouseEvent.Position;
AddChild(abdullah);
}
}
// private PackedScene packedScene;
// public override void _Ready() => packedScene = GD.Load<PackedScene>("res://Abdullah.tscn");
//
// public override void _UnhandledInput(InputEvent @event) {
// if (@event is InputEventMouseButton mouseEvent)
// //Create instance of Sprite scene
// if (packedScene.Instantiate() is Sprite2D abdullah) {
// abdullah.Position = mouseEvent.Position;
// AddChild(abdullah);
// }
// }
}

@ -7,10 +7,4 @@
script = ExtResource("1_4gg20")
[node name="Abdullah" parent="." instance=ExtResource("1_0rt2v")]
position = Vector2(540, 144)
[node name="Abdullah2" parent="." instance=ExtResource("1_0rt2v")]
position = Vector2(206, 268)
[node name="Abdullah3" parent="." instance=ExtResource("1_0rt2v")]
position = Vector2(855, 424)
position = Vector2(504, 310)