检查点
本课属于 Coddy Godot 学习旅程的“信号与碰撞”单元。第 20 课,共 27 课。
检查点会改变尖刺把你送到的位置。因此,起点会成为 main.gd 顶部的一个变量,所有函数都可以访问它:
var start_point = Vector2(80, 280)
func _on_spikes_body_entered(body):
body.position = start_point尖刺会把你送到 start_point 此刻保存的位置。更改这个变量,下次被击中时就会把你送到新的位置。
用 start_point = ... 来更改它。在函数内写 var start_point = ... 会创建一个新变量,它只在该函数内存在。顶部的那个变量保持不变。
挑战
中等把旗帜设为检查点。
- 在
_on_checkpoint_body_entered中,将start_point设为$Checkpoint.position,不要加var。 - 把旗帜变成薄荷绿:
$Checkpoint.modulate = Color(0.49, 0.95, 0.75)。
按“开始”,触碰旗帜,再碰到尖刺:你会回到旗帜处。然后点击“检查”。
亲手试试
extends Node2D
var start_point = Vector2(80, 280)
func _ready():
$Spikes.body_entered.connect(_on_spikes_body_entered)
$Checkpoint.body_entered.connect(_on_checkpoint_body_entered)
func _on_spikes_body_entered(body):
body.position = start_point
func _on_checkpoint_body_entered(body):
pass
信号与碰撞中的所有课
自己练习: Godot 在线编辑器