0
votes

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.

1

1 Answers

0
votes

You are removing bullets

_bullets[j].parent.removeChild(_bullets[j]);

Similarly

_enemies[i].parent.removeChild(_enemies[i]);

Use it conditionally when the objects x or y property goes beyond the stage boundaries

For eg, assuming the coordinates of enemy start from 0,0

   // _enemies[i].x > stage.stageWidth
   // _enemies[i].y > stage.stageHeight
   // _enemies[i].x +_enemies[i].width  < 0
   // _enemies[i].y + _enemies[i].height < 0

Although a better way would have been to add a method to the enemy class which removes every child in its display list, first. After that you could remove it from the stage & finally it's reference from the array.

To remove the array reference simply set the array element to null,

_enemies[i] = null;

The garbage collector will take care of removing unreferenced objects from the memory.