Hello, I’m creating a Space Invaders style game and I’ve been stuck on this bug for quite a while. When you start the game, it will spawn enemies like this:
The bug occurs when you defeat all of the enemies. The game is supposed to simply respawn new ones in the same pattern, but there is a bug causing them to spawn higher and to the left like this:
The enemies are created with a createEnemies
method:
'createEnemies(){
for(var y = 0; y < 3; y++) {
for(var x = 0; x < 6; x++) {
var enemy = this.enemies1.create((1.3*x + 2) * (550 / 7) + 30, ((1.5*y + 2) * 50) - 35, this.getEnemyString(displayed[0]));
this.enemies.add(enemy);
enemy.setScale(.6);
enemy.flipY = true;
enemy.body.velocity.x = -90
}
}
// timer to make enemies change direction
this.enemyTimer = this.time.addEvent({
delay: 1500,
callback: this.changeEnemyDirection,
callbackScope: this,
loop: true
});
}'
For the first call to the function (when the enemies are created initially), it is simply called in the create()
method. When it is called again, it is called in an enemyReset()
method, where it and many other things are reset.
` enemyReset() {
this.projectiles.clear(true, true);
this.enemyProjectiles.clear(true, true);
this.enemies.clear(true, true);
this.enemies1.clear(true, true);
this.enemies2.clear(true, true);
this.enemies3.clear(true, true);
this.enemies4.clear(true, true);
this.enemyTimer.remove();
this.createEnemies();
this.enem_defeated = 0;
this.defeatedText.text = "Enemies Defeated: " + this.enem_defeated;
}`
I can’t seem to figure out what’s wrong because the createEnemies()
method seems straightforward enough and it’s being placed in a constant value location.