Reputation: 510
I have a problem in detecting collisions in my game.
The game has an Enemy scene (kinematicbody2d) that has a function for assigning rectangle shape to the collisionshape2d
func set_collision(extents):
var collision = $Collision
var rect_shape = RectangleShape2D.new()
rect_shape.extents = extents
collision.shape = rect_shape
This function is called inside initialize function.
func initialize(start_position, ptexture, pspeed,
ppower, collision_extents):
position = start_position
$Sprite.texture = ptexture
speed = pspeed
power = ppower
set_collision(collision_extents)
I am using this function inside a Spawner scene to define multiple enemies with different properties.
func spawn_enemy():
var enemy = enemy_scene.instance()
var type = randi() % 4
spawn_positions.shuffle()
var random_position = spawn_positions[0].position
match type:
TYPES.BLACK:
enemy.initialize(random_position, enemy_black,
50, 4, Vector2(30, 23))
TYPES.RED:
enemy.initialize(random_position, enemy_red,
100, 3, Vector2(30, 23))
var main = get_tree().current_scene
main.add_child(enemy)
What happens is when the bullet from the player misses the first enemy (e.g. black one), the following black enemies can't be killed (queue free from scene).
However, if the player kills the first enemy, he can kill the following ones.
Upvotes: 0
Views: 78
Reputation: 510
None of this was wrong.
The problem was in the function _on_Bullet_body_enter(body)
I was checking body by name using body.name == "Enemy"
but sounds that this name is changing when multiple copies enter the scene.
I used is_in_group("enemies")
and it worked fine.
Upvotes: 0