Reach the Goal
Part of the Game Rules section of Coddy's Godot journey. Lesson 22 of 29.
A game you can lose needs a way to win. Often it is a place, like a flag at the end of a level.
The flag here is an Area2D called Goal. When a body walks in, it sends body_entered with that body. The player is in the player group, so the goal can tell it apart from other bodies:
func _on_goal_body_entered(body):
if body.is_in_group("player"):
$HUD/Message.text = "You win!"The HUD has an empty Message label across the middle of the screen.
Challenge
EasyLet the player win by reaching the flag.
- In
_ready(), connect$Goal.body_enteredto_on_goal_body_entered. - In
_on_goal_body_entered(body), when the body is in theplayergroup, set$HUD/Message.textto"You win!".
Press Play and walk to the flag: You win! appears across the screen. Then Check.
Try it yourself
extends Node2D
func _ready():
pass
All lessons in Game Rules
Practice on your own: Godot playground