The issue I’m having is that when my player collision with an enemy with his shield, it should destroy the enemy only. Instead it deletes every enemies in my array. The function called to remove the enemy is used elsewhere and it works correctly.
This is the collision check function:
function collisionCheck(){
enemyLoop :for(var i:int = level.enemies.length-1; i>=0; i--){
if(!shield){
removeEnemy(i, true);
removePlayer();
return;
}
else{
removeEnemy(i, false); //This is what is called when I get this issue
return;
}
}
The remove enemy function:
public function removeEnemy(enemyNum:int, playerDown:Boolean){
if(!playerDown){
killsMade++;
if(level.levelObjects.contains(level.enemies[enemyNum].healthBar)){
level.levelObjects.removeChild(level.enemies[enemyNum].healthBar);
}
level.enemies[enemyNum].enemyHit(true);
level.enemies[enemyNum].gotoAndPlay("Explosion");
explodedEnemyNum = enemyNum;
playExplosionSound();
explosionDone();
}
This is the function that remove the enemy sprite
public function explosionDone(e:TimerEvent){
stopExplosionSound();
if(level.enemies[explodedEnemyNum] != null){
level.levelObjects.removeChild(level.enemies[explodedEnemyNum]);
level.enemies.splice(explodedEnemyNum, 1);
}
}
Thanks for the help, let me know if you need anything else.
This issue has been corrected. The problem was with my collision check and the explosion done functions. Since the enemies were removed only after the explosion animation, it would still go in the loop for the collision. I corrected that by removing the enemies right away in the removeEnemy function.