Menu
CoddyTech

定时出现的管道

本课属于 Coddy Godot 学习旅程的“项目”单元。第 24 课,共 36 课。

接下来是管道。管道是一个场景 pipe.tscn:由上半部分和下半部分组成,中间留有空隙。在 pipe.gd 中,它会以每秒 SPEED 像素的速度向左移动。移出左边缘后,它会删除自身,这样旧管道就不会越积越多:

position.x -= SPEED * delta
if position.x < -60:
	queue_free()

这个场景有一个名为 PipeTimer 的 Timer,每隔 1.6 秒触发一次。在 main.gd 中,连接它的 timeout 信号,并在右边缘之外创建一个管道:

func _on_pipe_timer_timeout():
	var pipe = PIPE.instantiate()
	pipe.position = Vector2(700, 180)
	$Pipes.add_child(pipe)

Godot 按照场景树的顺序绘制节点。Pipes 位于 Ground 之前,因此地面会遮住每个管道的底部。

challenge icon

挑战

中等

让管道从屏幕上穿过。

  1. 在 pipe.gd 中,添加 const SPEED = 160。每个物理帧都让管道向左移动,并在 position.x 小于 -60 时将其移除。
  2. 在 main.gd 中,添加 const PIPE = preload("res://pipe.tscn"),并连接 $PipeTimer.timeout。
  3. 每次超时时,在 Pipes 节点中的 (700, 180) 位置创建一根管道。

按下“开始”:管道会从右侧滑入。然后按“检查”。

亲手试试

extends Node2D

func _ready():
	pass

项目中的所有课

自己练习: Godot 在线编辑器