[Godot] AddChild(), RemoveChild()
Godot 2023. 9. 26. 17:14 |반응형
Instantiate로 생성한 씬을 반복 재사용해 보자.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
|
using Godot;
public partial class Control : Node
{
public PackedScene Scene;
public Timer timer;
public Sprite2D Sprite;
public override void _Ready()
{
// C# has no preload, so you have to always use ResourceLoader.Load<PackedScene>().
Scene = ResourceLoader.Load<PackedScene>("res://Player.tscn");
Sprite = Scene.Instantiate<Sprite2D>();
timer = new Timer();
timer.Connect("timeout", Callable.From(OnTimeOut));
timer.WaitTime = 1.0;
AddChild(timer);
timer.Start();
}
public override void _Process(double delta)
{
}
public void OnTimeOut()
{
Sprite.Position = new Vector2(GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.X),
GD.RandRange(0, (int)GetViewport().GetVisibleRect().Size.Y));
if (Sprite.GetParent() == null )
{
AddChild(Sprite);
// Adds a child node. Nodes can have any number of children, but every child must have
// a unique name. Child nodes are automatically deleted when the parent node is deleted,
// so an entire scene can be removed by deleting its topmost node.
}
else
{
RemoveChild(Sprite);
// Removes a child node. The node is NOT deleted and must be deleted manually.
}
}
}
|
RemoveChild()로 제거한 자식 노드는 삭제되지는 않는다. AddChild()로 다시 추가 할 수 있다.
반응형
'Godot' 카테고리의 다른 글
[Godot] RayCast2D C# Example (0) | 2023.09.27 |
---|---|
[Godot] Area2D Gravity 중력 (0) | 2023.09.27 |
[Godot] Instantiate Scenes From Code 코드로 씬 생성하기 (0) | 2023.09.26 |
[Godot] Global Class Member Variables 전역 클래스 멤버 변수 만들기 (0) | 2023.09.25 |
[Godot] Access Another Script 다른 스크립트에 접근하기 (0) | 2023.09.25 |