Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Zrag |
Hi guys!
I’m instantiating the enemies using PackedScene.instantiate(), setting up his position with a PathSpawnLocation and, after that, adding the node to the main scene using add_child()
The firsts few spawns correctly but after some time, the mobs start appearing in pairs, or even 3 or 4 at a time. But it doesn’t add a child node. It means that if I have 4 enemies in screen with their respectives 4 nodes and suddenly it spawns 3 at the same time, it won’t appear 7 different nodes, just 5 nodes.
To make the things weirdest, some time the mobs spawn rotated, when I don’t use anything to rotate the enemies.
Those are the codes I’m using:
Mob:
func _ready():
$EnemyAnimation.play()
func set_animation(is_moving, mob_position, player_position):
if is_moving:
$EnemyAnimation.animation = "walk"
else:
$EnemyAnimation.animation = "idle"
$EnemyAnimation.flip_h = mob_position > player_position
Spawn method in main:
func _on_mob_timer_timeout():
var mob = mob_scene.instantiate()
var mob_spawn_location = get_node("MobPath/MobSpawnLocation")
mob_spawn_location.progress_ratio = randf()
mob.position = mob_spawn_location.position
add_child(mob)
Edit: after some additional test, I found that is not duplicating the sprites, but making their randomly their position into a new position, near to the new child.