I have a for loop that detects if an Enemy hits a bullet or the player. Those hit tests work fine but when the enemy leaves the stage I can't figure out how to remove the enemy from the array.
Here's the for loop
for (var i:int = _enemies.length - 1; i >= 0; i--){
for(var j:int = _bullets.length - 1; j >= 0; j--){
if(_bullets[j] != null && _enemies[i] != null){
//Check if bullet hits enemy
if(_bullets[j].hitTestObject(_enemies[i])){
//Tells enemy he's hit
if (_enemies[i] != null && _bullets[j] != null){
_enemies[i].isHit(_bullets[j].getType());
if(_enemies[i].getHealth() <= 0){
_enemies.splice(i, 1);
}
}
//removes bullet
if(_bullets[j] != null){
if (_bullets[j].parent) {
_bullets[j].parent.removeChild(_bullets[j]);
_bullets.splice(j, 1);
}
}
if(_bullets[j] != null){
if(_bullets[j].x > _stage.stageHeight){
if (_bullets[j].parent) {
_bullets[j].parent.removeChild(_bullets[j]);
_bullets.splice(j, 1);
}
}
else{
_bullets.splice(j, 1);
}
}
}
//Check if player hit
else if(_enemies[i] != null && _player != null){
if(_enemies[i].hitTestObject(_player)){
if (_enemies[i] != null){
_enemies[i].isHit("player");
_enemies.splice(i, 1);
}
else if (_enemies[i] == null){
_enemies.splice(i, 1);
}
}
}
if (_enemies[i] != null){
if(_enemies[i].isDead == true){
_enemies.splice(i, 1);
}
}
else if(_enemies[i] != null){
if(_enemies[i].x < 0){
_enemies.splice(i, 1);
}
}
if(_stage.contains(_enemies[i])){
}
else{
_enemies.splice(i, 1);
}
}
}
}
_stage is a reference of the stage from the main class.