Godot函数

await get_tree().create_timer(3).timeout
queue_free()

等待三秒后,

对自身及其节点树下子进行销毁 / 摧毁节点


@export var bullet_scene : PackedScene

获取bullet场景,需要在检查器里赋予快速加载Bullet场景 / 生成PackedScene场景


@export var animator : AnimatedSprite2D

获取动画,同样需要在检查器里赋予同场景下的AnimatedSprite


if not is_game_over:

# print(Input.get_vector("left","right","up","down") * move_speed)

velocity = Input.get_vector("left","right","up","down") * move_speed

if velocity == Vector2.ZERO:

animator.play("idle")

else:

animator.play("run")

判断移动速度来播放动画


func _on_body_entered(body: Node2D) -> void:

if body is CharacterBody2D:

body.game_over()

在怪物代码里,用信号 函数作碰撞判断,判断是否与角色碰撞


func _on_area_entered(area: Area2D) -> void:

if area.is_in_group("bullet"):

给bullet建立场景组,同是用信号 函数作碰撞判断,判断是否与子弹碰撞


func _physics_process(delta: float) -> void:

position += Vector2(bullet_speed, 0) * delta

增加自身的移动速度


func _on_fire() -> void:

if velocity != Vector2.ZERO or is_game_over:return

var bullet_node = bullet_scene.instantiate()

bullet_node.position = position + Vector2(14, 7)

get_tree().current_scene.add_child(bullet_node)

角色树里添加一个Timer,信号里添加函数,timeout()

判断是否移动及是否游戏结束。返回

初始化节点,获取一个新bullet,

获取角色位置+偏移位置,赋予bullet位置

加载当前场景,添加子场景/节点



自动生成

在主场景添加一个Timer节点,设置简隔时间3.0S,自动开始启用。

然后在点节点信号 使用timeout()


@export var spawn_timer : Timer

func _on_timer_timeout() -> void:

var slime_node = slime_scene.instantiate()

slime_node.position = Vector2(260, randf_range(50, 115))

get_tree().current_scene.add_child(slime_node)

公开timer变量,在检查器加载节点树下的timer

在timeout函数里,初始化一个新的slime,设置位置,然后添加到场景树下



Canvas Layer 用来添加界面UI

AudioStreamPlayer


Timer 

timer.start()

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容