Hey Everyone,

I'm trying to remove a child of a Sprite in actionscript, where each of the children are stored in arrays as well as holder sprite, as that seemed to easiest way to go about things. The arrays are then looped through for each of the children.

However, often while testing my game, I get an error:
ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller.
at flash.display::DisplayObjectContainer/removeChild()
at TowerDefence_fla::MainTimeline/moveBullets()
at TowerDefence_fla::MainTimeline/everyFrame()


my moveBullets function is:
Actionscript Code:
function moveBullets():void {
    for each (var bullet in bulletArray) {
        //if (bullet.bulletType = ...)
        bullet.y += bullet.ySpeed;
        bullet.x += bullet.xSpeed;
        bullet.stageTime += 1;
        for each (var enemy in enemyArray) {
            if (bullet.hitTestObject(enemy)) {
                enemy.health -= bullet.damage;
                if (enemy.health <= 0) {
                    enemyHolder.removeChild(enemy); //error here :C
                    enemyArray.splice(enemyArray.indexOf(enemy), 1);       
                }
                bulletHolder.removeChild(bullet);
                bulletArray.splice(bulletArray.indexOf(bullet), 1);
            }
        }
        if (bullet.stageTime > bullet.range / bullet.speed) {
            bulletHolder.removeChild(bullet);
            bulletArray.splice(bulletArray.indexOf(bullet), 1);
        }
    }
}

I can't figure out why this happens. When it does happen, the Array is spliced fine because the enemy stops moving.

Another problem I have had is that when I remove the child like that, the loop runs for 1 less time then it should, so sometime the enemies or bullets pause for a frame. What's the best way to get around this. It's possible the problems are related.

Any help would be appreciated. Thank you!