0
votes

So i have a list of enemies instantiated in a array and i have them on screen. When they get shot, i want the enemy that has been shot to be removed from the screen. BTW the enemy Class has AS Linkage with the Enemy movieClip, same thing with the Bullet. I understand the problem is that it cant compare and remove but i dont know how to fix it. Basically i would love to know how can i remove instances of a class file that are stored in a array?

This is what i got so far:

    stage.addEventListener(MouseEvent.CLICK, shoot);

    var enemyList:Array = new Array();
    addEnemies(608.75, 371.85);

    function addEnemies(xLoc:Number, yLoc:Number):void {    
        var enemy:Enemy = new Enemy(xLoc, yLoc);
    addChild(enemy);
    enemyList.push(enemy);
    }

    function shoot(event:MouseEvent):void{
        for(var i:int = 0; i < 4; i++){
        var enemy:Enemy = enemyList[i];
            if(scope.redDot.hitTestObject(enemy)){
        trace("SHOT TO DEATH");
    }
    else{
        trace("DIDNT DIE");
    }
        }
    }

I Keep getting this error in the output window: TypeError: Error #1010: A term is undefined and has no properties. at sniper_fla::MainTimeline/shoot()[sniper_fla.MainTimeline::frame1:58]

Any help would be appreciated!

3

3 Answers

1
votes

A more complex but much faster method to remove items from that enemy array is to use pop():

function removeEnemy(enemy:Enemy):void
{
    var i:int = enemyList.indexOf(enemy);

    if(i >= 0)
    {
        if(enemyList.length === 1 || enemyList[enemyList.length] === enemy)
        {
            // If we are referring to the last enemy in the list, or there is only
            // one enemy in the list, we can just use pop to get rid of the target.
            enemyList.pop();
        }
        else
        {
            // In this case, we remove the last enemy from the array and retain a
            // reference to it, then replace the target enemy we want to remove
            // with that enemy.
            var tempEnemy:Enemy = enemyList.pop();
            enemyList[i] = tempEnemy;
        }
    }

    // We can also remove the Enemy from the stage in this function.
    enemy.parent && enemy.parent.removeChild(enemy);
}

This approach removes the need to re-index the entire array when you delete something from it, which will produce massive performance improvements if you are constantly removing and adding items to the enemy list. The downside to this approah is that the enemy list won't remain sorted, though I don't see a need for it to be sorted.

0
votes

Its been a long time since i have used AS3 but

enemyList.splice(enemyList.indexOf(enemy), 1)

should work for the removal

I am not sure about the error you are getting though

0
votes

@RustyH is correct:

enemyList.splice( enemyList.indexOf( enemy ), 1 );

But since you are doing it in a for loop with a constant evaluation ( i < 4 ) you can do this which is slightly faster:

enemyList.splice( i, 1 );

Also that null reference error you are getting:

TypeError: Error #1010: A term is undefined and has no properties. at sniper_fla::MainTimeline/shoot()[sniper_fla.MainTimeline::frame1:58]

That is most likely caused by your: scope.redDot.hitTestObject(enemy) particularly the scope or the child scope.redDot. One of those may not exist when you are trying to reference it. You'll have to thoroughly check your code, but this is the downside to coding in the timeline as it could be many different issues (or none of the following at all) such as:

  • redDot does not exist as a child of scope
  • redDot or scope (or both) have not been created on your current frame
  • scope or redDot are the incorrect reference names

This list goes on...again that's all speculation that the error is scope or scope.redDot.