Three Hearts
Part of the Projects section of Coddy's Godot journey. Lesson 30 of 33.
Touching the guard should cost a heart. The three hearts in the top bar are Heart1, Heart2 and Heart3, children of $HUD/Hearts. The guard is an Area2D, so its body_entered fires when it touches the hero:
func _on_enemy_body_entered(body):
health -= 1
$HUD/Hearts.get_child(health).visible = falseget_child(i) gives a node's child by its place, counting from 0. After the first hit health is 2, and get_child(2) is Heart3. The hearts disappear from the right.
body_entered fires once per touch. A hero who stays in the guard's path loses a heart each time it comes back.
Challenge
MediumIn main.gd:
- Add
var health = 3and connect$Enemy.body_entered. - On each touch, lower
healthby one, then hide$HUD/Hearts.get_child(health).
Try it yourself
extends Node2D
var has_key = false
func _ready():
$Key.body_entered.connect(_on_key_body_entered)
$DoorZone.body_entered.connect(_on_door_zone_body_entered)
func _on_key_body_entered(body):
has_key = true
$Key.queue_free()
$HUD/KeyIcon.visible = true
func _on_door_zone_body_entered(body):
if has_key:
$Door.queue_free()
$DoorZone.queue_free()
This lesson includes a short quiz. Start the lesson to answer it and track your progress.
All lessons in Projects
1Project: Breakout
Breakout PaddleKeep the Paddle on Screen4Project: Dungeon Crawler
Walk the DungeonPick Up the KeyPractice on your own: Godot playground