0
votes

Please, I need help trying to remove bullets and enemies from my stage. I am very new to programming. Thank you.

I receive the following error message:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Main/fl_EnterFrameHandler() at flash.utils::Timer/_timerDispatch() at flash.utils::Timer/tick()

The code where debug sent me.

package 
{

import flash.display.MovieClip;
import flash.events.Event;
import flash.utils.Timer;

public class Main extends MovieClip
{

    var enemyShipTimer:Timer;
    var coinTimer:Timer;
    var playerscore:Number = 0;
    var enemies:Array;
    var bullets:Array;

    public function Main()
    {
        enemyShipTimer = new Timer(1000);
        enemyShipTimer.addEventListener("timer", fl_EnterFrameHandler);
        enemyShipTimer.start();
        coinTimer = new Timer(1000);
        coinTimer.start();
        enemies = new Array ();
        bullets = new Array ();

    }

    function fl_EnterFrameHandler(event:Event):void
    {
        var enemyinstance = new enemy_ks();
        stage.addChild(enemyinstance);
        enemies.push(enemyinstance);
        trace(enemies.length);

        for (var count=0; count<enemies.length; count++)
        {

            for (var bcount=0; bcount<bullets.length; bcount++)
            {
                if (enemies[count].hitTestObject(bullets[bcount]))
                {
                    removeChild(enemies[count]);
                    enemies.splice(count, 1);
                    removeChild(bullets[bcount]);
                    bullets.splice(bcount, 1);
                }
            }
            score_ks.text = " " + playerscore;
        }


    }

}
}
1

1 Answers

0
votes

EDIT: Reread your code and noticed the real error is actually that you are adding to the stage but removing from your Main sprite. You need to match those up. You cannot remove an object from a parent if it is not actually a child of that object.

The points below still need to be addressed as well, otherwise you may end up with other errors.


Your issue is with your loop. In your loop, you adjust the array length on each successful hit test but you never adjust the count of the loop.

So think of it like this.

You start with:

count = 0;
length = 10;

Now say you run a loop for count < length and you splice at count == 4 and count == 7. In your current scheme, you will only hit the following objects (using the original index)

0 1 2 3 4 6 7 9

Notice that you don't hit index 5 or 8. When you modify the array like that and don't modify the count, you end up skipping over certain items. After splicing index 4, the original index 5 moves to 4 and everything else moves back one as well. So when you move to index 5, you are actually reading the original index 6.

Very simple fix for this is to just adjust your counts as you splice.

if (enemies[count].hitTestObject(bullets[bcount]))
{
    removeChild(enemies[count]);
    enemies.splice(count, 1);
    count--; //subtract 1 from the count
    removeChild(bullets[bcount]);
    bullets.splice(bcount, 1);
    bcount--; //subtract 1 from the bcount
}

That will ensure your count actually hits every object. You could also use a for each loop to handle this, though that type of loop is slower than a standard for loop depending on application.

Additionally, a DisplayObject can only be removed from a DisplayObjectContainer if it is actually a child of that container. If it is not, it will error out. So I believe you may also be running into an issue where your array does not fully line up with what is on the stage and you are trying to remove an object that doesn't actually exist as a child of the parent

As a minor aside, you should avoid adding children directly to the stage unless you have a real reason to do so. Instead, add it directly to the application object's display list using the this keyword, or simply addChild().