Spawn on a Timer
Part of the Game Rules section of Coddy's Godot journey. Lesson 19 of 29.
To drop a block every second, combine a Timer with your spawn() function.
The scene has a Timer called SpawnTimer. It runs out every second and starts on its own. Connect it:
func _ready():
$SpawnTimer.timeout.connect(_on_spawn_timer_timeout)
func _on_spawn_timer_timeout():
spawn(320)Change SpawnTimer's wait_time to make the game faster or slower.
Challenge
EasyDrop a block at x = 320 every second:
- In
_ready(), connect$SpawnTimer.timeoutto_on_spawn_timer_timeout. - Write
_on_spawn_timer_timeout(), which callsspawn(320).
Try it yourself
extends Node2D
var block_scene = preload("res://block.tscn")
func _ready():
pass
func spawn(x):
var block = block_scene.instantiate()
block.position = Vector2(x, -30)
add_child(block)
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Game Rules
4Spawning
A Packed ScenePlace the New NodeA Spawn FunctionSpawn on a TimerRandom PositionsRandom LanesPractice on your own: Godot playground