1 节点继承关系

2 脚本生命周期

  1. 创建节点
  2. 把节点加到树中
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
extends Sprite2D

var jishiqi:float = 5;

#节点添加到节点树的时候调用
func _enter_tree() -> void:

#先调用父节点,再调用子节点
print("enterTree")

# Called when the node enters the scene tree for the first time.
# 节点全部加载好后调用,所以比上面个方法晚一点,是冒泡调用
func _ready() -> void:

#先调用子节点,再调用父节点
print("ready")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:

#每帧调用
jishiqi -= delta
if(jishiqi<=0):
queue_free();

func _physics_process(delta: float) -> void:

#每次物理系统计算,会调用一次
pass

#节点离开节点树时调用
func _exit_tree() -> void:
print("exitTree")

3 监听键鼠输入

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
45
46
47
48
49
50
51
52
53
extends Node2D


# Called whenv the node enters the scene tree for the first time.
func _ready() -> void:

#鼠标设置,显示、隐藏在游戏窗口
#Input.mouse_mode = Input.MouseMode.MOUSE_MODE_CONFINED
pass

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:

#TODO,这有bug
#按键事件
if Input.is_key_pressed(Key.KEY_B):
print("按下了B键")

if Input.is_action_just_pressed("jump"):
print("按下了跳跃")
elif Input.is_action_pressed("jump"):
print("跳跃中")
elif Input.is_action_just_released("jump"):
print("松开跳跃")

#TODO获取按键向量
float s = Input.GetActionStrength("jump")

func _input(event: InputEvent) -> void:

#是键盘事件,并且按下了
if event is InputEventKey:

#按下的V键,参阅 @GlobalScope_Key 以获取键码常量列表。
if event.keycode == KEY_V:

#判断当前是否是持续按压
if event.is_echo():
print("持续按下了V键")

elif event.is_pressed():
print("V按下了瞬间")

elif event.is_released():
print("抬起V")

#鼠标事件
elif event is InputEventMouseButton:

if event.is_pressed():
print(event.position)
print(event.button_mask)

4 资源网站

推荐几个国外比较好的资源网站,国内的资源网站现在还比较稀缺,需要科学上网

  • Untiy Store ,最大的游戏资源网站,可以使用里面的2D图片资源
  • craftpix ,国外的一家提供高品质的优质和免费的2D游戏资产的网站
  • game dev market ,内容涉及2D、3D、音频和GUI素材,也可以作为一个寻找素材的补充
  • kenney ,国外一家做游戏创意原型的资源站
  • Fiverr ,一个综合的数字服务提供平台,可以直接在里面找到为我们工作的人,然后他们会提供一些我们定制的服务。

5 节点关系

根节点get_tree().root

查找子节点find_child(“xxxxxx”,true,false)

添加子节点xxxNode.add_child(node2D)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
extends Node2D

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if Input.is_action_just_pressed("jump"):

#还有个get_node()方法没演示
var childNode = get_tree().root.find_child("childNode",true,false)

#childNode.queue_free();

var node2D = Node2D.new()
node2D.name = "new"
childNode.add_child(node2D)


获取子节点

var subNode1 = $SubNode2
var subNode2 = $”SubNode2”

获取当前节点

var currentNode1 = $”.”
var currentNode2 = self

获取父节点

var parentNode1 = get_parent()
var parentNode2 = $”../“

6 场景切换

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
extends Node

@export var newScene:PackedScene

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
pass # Replace with function body.

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
if Input.is_action_just_pressed("left"):

#跳转场景1
#get_tree().change_scene_to_file("res://scen_2.tscn")

#跳转场景2
#get_tree().change_scene_to_packed(newScene)

#跳转场景3
get_tree().current_scene.add_child(newScene.instantiate())

7 自动加载

很方便的做出一个,切换场景也不会被销毁的单例形式脚本

image-20251127030726244

8 向量

点乘的意义

image-20251127231744602

9 节点的常用属性和方法

Node2D
canvasItem属性

z Index:越大越在上

10 用脚本修改节点

例1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
extends Sprite2D

@onready var sprite_2d: Sprite2D = $"."

# Called when the node enters the scene tree for the first time.
func _ready() -> void:

sprite_2d.visible = true
sprite_2d.z_index = 10
sprite_2d.z_as_relative =false

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:

#获取鼠标位置
var pos = get_global_mouse_position()

#看向某个点
look_at(pos)
print(pos)

例2操作sprite

1
2
3
4
5
6
7
8
9
10
11
12
13
extends Sprite2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:

#同样也可以用导出一个属性的方式,加载图片
texture = load("res://icon.svg")
centered = false
# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass

11 物品分组

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
extends Sprite2D


# Called when the node enters the scene tree for the first time.
func _ready() -> void:
var enemys = get_tree().get_nodes_in_group("敌人")

#TODO,没生效,作用1,分组管理
if Input.is_action_just_pressed("left"):
enemys.any(queue_free)

#TODO,没生效,作用2,分组消息
if Input.is_action_just_pressed("right"):
get_tree().call_group("敌人","test")

# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(delta: float) -> void:
pass


12 自定义信号

//TODO跳过

13 动画

animatedSprite节点

播放多个图片形成动画

AnimationPlayer节点

功能更多

14 灯光&声音

阴影,遮罩

15 碰撞

16 射线形状检测

17 帧和物理帧

18 物理效果

19 角色控制

CharacterBody2D节点

UI

20 游戏界面基类 &图像&文字&按钮

锚点和轴心点

焦距

21 进度条&输入框

22 自动布局

23 2D相机

24 瓦片

25 2D寻路

26 粒子系统

27 存档

28 数据

29 网络