I am creating a tower defense game and upon the aoe tower destroying 2 monsters at the same time i get 'TypeError: Error #1009: Cannot access a property or method of a null object reference...at Document/loop()... at EnemyRed/removeSelf()'
I am using Event.ENTER_FRAME function in the document class to check if (monster hp < 0), then call a function within the monster class to remove itself from its parent. (Code below).
The trace shows that it is trying to remove the same instance twice even though it should have been removed from the array already.
Document class:
public function loop(event:Event):void
{
//If enemy hit points < 0 remove self
if(enemies.length > 0)
{
for(var e = 0; e < enemies.length; e++)
{
if(enemies[e].hitPoints <= 0)
{
enemies[e].removeSelf();
Monster class:
public function removeSelf():void
{
trace(this.name);
trace(this.parent);
removeEventListener(Event.ENTER_FRAME, loop);
parent.removeChild(this);
Document.enemies.splice(this,1);
}
trace returns:
Monster: instance2019
Parent: [object Level1]
Monster: instance2019
Parent: null