1
votes

I'm trying to make a spaceship game and I'm having a problem when I'm using the removeChild() method to remove the projectiles or the enemy ships out of the stage.

Here's the relevant part of the code:

var bulletsArr:Array = new Array();
var eShipsArr:Array = new Array();


for (var c = 0; c < eShipsArr.length; c++){
stage.addChild(eShipsArr[c]);
}
....

stage.addEventListener(KeyboardEvent.KEY_DOWN,shoot);

function shoot (e:KeyboardEvent):void{
if(e.keyCode==17){
    var pj:projectile = new projectile();
    pj.x = SShip.x;
    pj.y = SShip.y;
    stage.addChild(pj);
    bulletsArr.push(pj);
    if(bulletsArr.length >= 10){
       bulletsArr = bulletsArr.slice(1,10);
    }
}
}
stage.addEventListener(Event.ENTER_FRAME,checkHit);

function checkHit (e:Event):void{
for (var d = 0; d < bulletsArr.length; d++){
    for(var f = 0; f < eShipsArr.length; f++){
        if(bulletsArr[d].hitTestObject(eShipsArr[f])){
            trace(bulletsArr[d].parent);
            stage.removeChild(bulletsArr[d]); //ArgumentError: Error #2025!!!!
        }
    }
}
}

When I use removeChild() with stage, the trace returns null. Without it the trace returns Stage object. In both cases the same error jumps.

1

1 Answers

0
votes

Could it be that you are trying to remove a child that was already removed?

You are not removing it from the array, so the next loop or the next checkHit call will include checks on objects that were possibly already removed from stage. A hitTestObject check would return true even with objects that are not on the stage.

Untested (note that the bulletes array is being iterated backwards to avoid problems with the length when removing array elements):

for (var d = bulletsArr.length - 1; d >= 0; d--){
    for(var f = 0; f < eShipsArr.length; f++){
        if(bulletsArr[d].hitTestObject(eShipsArr[f])){
            trace(bulletsArr[d].parent);
            stage.removeChild(bulletsArr[d]); 
            bulletsArr.splice(d, 1); // remove bullet from array
        }
    }
}

Edit: Here's some untested (properly typed) code in case it is possible that a single bullet can collide with two or more ships, and all you need to do is remove the bullets:

for(var f:Number = 0; f < eShipsArr.length; f ++)
{
    for(var d:Number = bulletsArr.length - 1; d >= 0; d --)
    {
        if(bulletsArr[d].hitTestObject(eShipsArr[f]))
        {
            stage.removeChild(bulletsArr[d]);
            bulletsArr.splice(d, 1);
        }
    }
}

The loops where interchanged, that way removed bullets are not checked again.

In case it is possible that a single bullet can collide with two or more ships, and you are planning to do something with ships that collided with the bullet too, then try this:

    for(var d:Number = bulletsArr.length - 1; d >= 0; d --)
    {
        var bullet:DisplayObject = bulletsArr[d];
        var bulletHitSomething:Boolean = false;

        for(var f:Number = 0; f < eShipsArr.length; f ++)
        {
            if(bullet.hitTestObject(eShipsArr[f]))
            {
                if(!bulletHitSomething)
                {
                    bulletHitSomething = true;
                    stage.removeChild(bullet);
                    bulletsArr.splice(d, 1);
                }
                // do other, ship specific stuff here
            }
        }
    }

The current bullet object reference is stored in a variable, and all the checks are done against that variable instead of the element in the array using array access. Unlike the array access method where it coule possibly access a non existent index, this way possible consecutive checks on the same bullet should be fine. Also a variable stores whether the bullet already hit something, so that it doesn't try to remove that bullet again.