Dynamically created Area2D not detecting area_entered
Asked Answered
C

3

0

Hi,

I'm practicing game development by creating a Flappy Bird clone. I've tried to program everything without using sprites or assets. And have so far completed making the top and bottom pipes with a gap between them. Now, I'm trying to create an Area2D node between the pipes to create a score counting system. However, what I have programmed doesn't seem to detect the player entering or exiting it.

The logic I'm trying to implement is as follows:

  • 1. The top and bottom pipes are created using KinematicBody2D and CollisionPolygon2D during runtime and added as a child of a Node2D called "Obstacles".

  • 2. The Area2D is also created at runtime using Area2D and CollisionPolygon2D and added as a child of the top pipe to fill the gap between the top and bottom pipes.

  • 3. When the player enters the gap the Area2D detects it and waits for the player to exit the area to update the score.

I've tried using signals to print something on the console when the player enters the area but there's no output at all. I'm not sure how to tackle this. Any ideas?

`func add_point_area():

pointArea = Area2D.new()

var pointAreaShape = CollisionPolygon2D.new()

var pointAreaPolygons = PoolVector2Array()

pointAreaPolygons.push_back(Vector2(topPipeCreationPosition.x - pipeWidth / 2, topPipeHeight))

pointAreaPolygons.push_back(Vector2(topPipeCreationPosition.x + (pipeWidth / 2), topPipeHeight))

pointAreaPolygons.push_back(Vector2(bottomPipeCreationPosition.x + pipeWidth / 2, topPipeHeight + gapBetweenPipes))

pointAreaPolygons.push_back(Vector2(bottomPipeCreationPosition.x - (pipeWidth / 2), topPipeHeight + gapBetweenPipes))

pointAreaShape.set_polygon(bottomPipePolygonPoints)

pointArea.add_child(pointAreaShape, true)

pointArea.monitoring = true

topPipeBody.add_child(pointArea, true)

pointArea.emit_signal("area_entered")

pointArea.connect("area_entered", $".", "on_area_entered")`

Here's my runtime scene tree.

Correna answered 13/12, 2023 at 7:56 Comment(0)
C
0

I think I found the solution.

The problem was that my player didn't have an Area2D node attached to it. So, I added an Area2D node to my player and it's detecting the player entering and exiting now.

Player Scene Tree

Correna answered 13/12, 2023 at 8:32 Comment(0)
P
1

area_entered is triggered when another area enters this area. please connect to the body_entered signal if you would like to detect a body entering the area. additionally, please check if the area has "detect bodies" flag enabled.

https://docs.godotengine.org/en/stable/classes/class_area2d.html

Perturbation answered 13/12, 2023 at 11:28 Comment(0)
C
0

Perturbation Thank You! I didn't realize there was a body_entered signal available too.

Correna answered 22/12, 2023 at 15:18 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.