Probably just something dumb but I'm rewriting Galaga in p5.js for a university assignment and towards the beginning I'm at destroying enemies. When a bullet is fired and it hits say the third enemy on the screen, all of the enemies before it are being destroyed.
The enemies array is just a simple array where enemy objects are pushed: enemies = [];
The destroy function for the enemies is
this.die = function() {
enemies.splice(enemies[this.index], 1);
}
And the loop is
for( var b = 0; b < player.bullets.length; b++) {
for(var i = 0; i < enemies.length; i++) {
var bullet = player.bullets[b];
if(collision(bullet.x, enemies[i].x, bullet.y, enemies[i].y, 20, 55, 40, 55)) {
enemies[i].die();
}
}
}
Any help would be great