Coin Collector: Coins
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 24 of 27.
Each of the six coins runs coin.gd, so you write it once. A coin does three things you already know:
- It joins the group
coins, so Main can count the coins later. - It listens to its own
body_entered. - It frees itself when the player touches it.
The player is in the group player, set in the scene. Check for it before freeing the coin, so nothing else picks one up:
func _on_body_entered(body):
if body.is_in_group("player"):
queue_free()Challenge
EasyWrite coin.gd:
- In
_ready(), join the groupcoinsand connectbody_enteredto_on_body_entered. - In
_on_body_entered(body), free the coin if the body is in the groupplayer.
Walk over the coins. Each one should disappear.
Try it yourself
extends Area2D
func _ready():
pass
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Signals and Collisions
6Project: Coin Collector
Coin Collector: WalkCoin Collector: CoinsPractice on your own: Godot playground