If your ball is a kinematic body, then area_entered
signal won't get triggered because that signal is for other areas only. You should keep using body_entered
.
the ball goes through the goal, but the level does not change
To me, it seems like either the signal is not being emitted, or the condition (is_in_group
) is not true. Make sure that the signal and the condition are working properly using print()
.
So you can try something like this:
extends Sprite
func _on_Hitbox_body_entered(body) -> void:
print("Signal Triggered!")
if body.is_in_group("ObjectGroup"):
print("Condition Passed!")
get_tree().change_scene("res://Level" + str(int(get_tree().current_scene.name) + 1) + ".tscn")
You can also use assert
to check if the condition returns true or not, however that'll pause the execution if the condition happens to return false.
assert(body.is_in_group("ObjectGroup"))
Hopefully that'll give you a better idea of what the issue is.
If the issue is with the signal:
- Make sure you're using the correct signal (
body_entered
).
- Make sure the signal is connected properly (you should see a green signal icon to the left side of the method).
- Check the collision layer/mask of both the goal area and the ball body.
Here's a short 10min video by GDQuest about layers and masks, if you're not familiar with them.
If the issue is with the condition, you can either double check your groups and make sure that everything is set up properly or instead of checking the group, check the class.
To do that, load/preload your ball script into a variable or constant. Then use is
to check if body extends the ball script:
extends Sprite
const Ball = preload("ball.gd")
func _on_Hitbox_body_entered(body) -> void:
print("Signal Triggered!")
if body is Ball:
print("Condition Passed!")
get_tree().change_scene("res://Level" + str(int(get_tree().current_scene.name) + 1) + ".tscn")
Alternatively, you can use the class_name
keyword in your ball script if you don't want to load it into a variable first.
extends KinematicBody2D
class_name Ball
However, that'll make ball.gd
become available when adding new nodes to the scene because class_name
adds the script as a new type to the editor.