Areas Meet Areas
Part of the Signals and Collisions section of Coddy's Godot journey. Lesson 6 of 27.
Godot's nodes come with signals built in. An Area2D has four you will use often:
body_enteredandbody_exited: a body came in or left. Bodies are solid things, likeCharacterBody2DandStaticBody2D.area_enteredandarea_exited: another area came in or left.
An area never sends body_entered for another area. The falling stars are areas, so the basket needs area_entered:
func _ready():
area_entered.connect(_on_star_caught)The function gets the area that came in, so it takes one value, like _on_body_entered(body). You can name it anything.
Challenge
EasyMake the basket notice the falling stars.
- The stars are areas, not bodies. In
_ready(), connectarea_enteredin place ofbody_entered. - Keep
_on_star_caughtas the function.
Press Play: the Console shows Caught a star! twice, once per star. Then Check.
Try it yourself
extends Area2D
func _ready():
body_entered.connect(_on_star_caught)
func _on_star_caught(star):
print("Caught a star!")
All lessons in Signals and Collisions
2Signals
Areas Meet AreasYour Own SignalSignals With ValuesAnother Node's SignalListen to Your SignalPractice on your own: Godot playground