Area2D signal triggered when bodies are instanciated

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Patrick_W

I made a scene that is a Node2D.
It inherits a sprite and an Area2D with a circular collision shape that is the size of the sprite.
The Area2D is connected to ‘_on_Area2D_body_entered(body)’

func _on_Area2D_body_entered(body):
body.takeDamage(destructorDamage, false)

When a body enteres the area it gets damage.
This scene is then put into my player scene.

The level scene is basically a box with walls around it so the player has to stay within that box.
Outside the box I dynamically instanciate enemy scenes in the level script that move into the box.

    const enemy1 = preload("res://Classes/Enemies/PirateEnemy1.tscn")

	for i in range(7):
	var ship1 = enemy1.instance()
	ship1.global_position.x = $Position2D.global_position.x + 300*i
	ship1.global_position.y = $Position2D.global_position.y

The problem is that the first instanciated enemy triggers the area’s body entered signal and takes damage.
After this, everything is behaving as it should.

Weirdly enough, when I dont instanciate enemies in code but put them into the level scene in the editor eveything behaves as expected. I have no idea what I am doing wrong.

I made a little video that illustrates the issue:

youtube video

I am not sure what the problem here is. Could you upload a test file so that we can check? The problem might be with the “takedamage” function. Maybe you cause damage to all enemies instead of the instance that got hit.

johnygames | 2019-12-28 17:15

Did you find answer