2
votes

I made a game with AS3 where you must click on falling bombs before they explode and destroy a wall. Now, I'm trying to remove the bombs that fell just when the wall got destroyed, so I did a removeChild(blob) in my game over function, since these bombs are added to the stage with an addChild(blob), and I keep getting this error:

ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. ...line 80

...And by the way, I've already tried things like these:

this.parent.removeChild(this);

or

blob.parent.removeChild(blob);

or

stage.removeChild(blob);

but I still get the same error.

Here's my code:

package cem {

    import flash.geom.*;
    import flash.display.*;
    import flash.events.*;
    import flash.display.MovieClip;
    import flash.utils.Timer;



    public class Jeu extends MovieClip {

        //Variables publiques
        var decor: MovieClip = new Decor();
        var chrono: cem.Chronometre;
        var nextObject: Timer = new Timer(800, 0);
        var _menu: MovieClip = new Menu();
        var _btnJouer: MovieClip = new BoutonJouer();
        var blob: cem.Blob;
        var score: Score;

        public function Jeu() {
            // constructor code

            //***********************************************Mettre menu***********************************************//
            addChild(_menu);

            addChild(_btnJouer);
            _btnJouer.x = 500;
            _btnJouer.y = 500;
            _btnJouer.addEventListener(MouseEvent.CLICK, jouer);
        }



        //*****************************************************Jouer**************************************************//
        function jouer(e: MouseEvent) {

            removeChild(_menu);

            addChild(decor);
            decor.gotoAndStop(1);

            chrono = new cem.Chronometre();
            addChild(chrono);
            chrono.demarrer();
            score = new Score();
            score.x = 600;


            nextObject.addEventListener(TimerEvent.TIMER, creerBlobs);
            nextObject.start();



        }

        //**************************************************Créer Bombes***********************************************//
        function creerBlobs(e: TimerEvent) {
            blob = new cem.Blob();
            blob.x = Math.floor(Math.random() * (stage.stageWidth - blob.width));
            addChild(blob);
            blob.gotoAndStop(1);
            blob.addEventListener("explosion", perdreVies);
        }

        //************************************************Perdre des vies*********************************************//
        public function perdreVies(e: Event) {
            decor.moinsVie();
            decor.addEventListener("gameisover", _gameOver);
        }

        //************************************************Partie terminée*********************************************//
        public function _gameOver(e: Event) {




            blob.removeEventListener("explosion", perdreVies);


            removeChild(blob);


            chrono.arret();
            addChild(_menu);
            addChild(_btnJouer);

            nextObject.stop();
            nextObject.removeEventListener(TimerEvent.TIMER, creerBlobs);
            nextObject.removeEventListener(TimerEvent.TIMER, creerBlobs);


            addChild(score);
            score.affichageScore.text = "votre score: " + chrono.secondes * 1000;
        }


    }
}
1

1 Answers

2
votes

The var name blob can only reference one specific Blob object at a time. Each time you create a new Blob you are reassigning the name blob to the last created one, losing the reference to the previous one.

That error says, at the time you call removeChild the specific Blob assigned to the name "blob" is not a child, it is not on the display list.

So its referencing the wrong Blob or its already removed from the display list.

To avoid errors u can also say something like if (blob.parent) remove child blob