views:

24

answers:

2

I am creating a pacman-style game. I am trying to remove an instance of a MovieClip using removeChild(). When the MovieClip instance "box" hits the MovieClip instance "circle" --circle will be removed from the stage.

I am receiving the following error below: ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Move/eatCircle()

package {
       import flash.display.Sprite;
       import flash.display.MovieClip;
       import flash.events.Event;
       import flash.events.KeyboardEvent;
       import flash.ui.Keyboard;

       public class Move extends MovieClip {

       var ScoreObjects:Array = new Array(); // creates ScoreObjects array


          private var inertia:int=8; //amount of friction

       var score_field:String;
       //var point:MovieClip;


     // Constructor--------------------------------------------------------------------
          public function Move() {
             init();
          }

     // function init -----------------------------------------------------------------
       function init():void {

             //stage.frameRate=60;
        var score_field:String="";

      ScoreObjects[0] = new Circle();
      ScoreObjects[0].amount = 1; // amount of point
      ScoreObjects[0].name = "circle";


             stage.addEventListener(Event.ENTER_FRAME, frameloop);
             stage.addEventListener(KeyboardEvent.KEY_DOWN,keyDownEvent);
             stage.addEventListener(KeyboardEvent.KEY_UP, keyUpEvent);

        box.addEventListener(Event.ENTER_FRAME, eatCircle);
        wall.addEventListener(Event.ENTER_FRAME, hitWall);

        stage.addChild(ScoreObjects[0]); // add Score Objects to stage ------------------------------
        trace(ScoreObjects[0]);

        ScoreObjects[0].x = 105;
        ScoreObjects[0].y = 233;

          }

     // function eatCircle --------------------------------------------------------------
     function eatCircle(event:Event):void {

      if (box.hitTestObject(ScoreObjects[0])) {
        trace ("I ate the circle");
        removeChild(ScoreObjects[0]);
        //calcScore();
       } else {
        trace ("I didn't eat the circle");
       }
     }



       }// end of class
    }// end of package
+2  A: 

I don't have an AS3 compiler at hand to test this, but since you did stage.addChild(ScoreObjects[0]) I believe you should do stage.removeChild(ScoreObjects[0])?

pberkes
@pberkes: Thanks, I was able to remove the circle off the stage--doing stage.addChild(ScoreObjects[0]) But, I'm not sure why I'm still receiving an error in the output window.(I know you mentioned you don't have an AS3 compiler, so not sure if this is something you might be able to answer. But thanks for trying to help.)ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Move/eatCircle()
jc70
A: 
if((ScoreObjects[0] as Circle)&&((ScoreObjects[0] as Circle).parent!=null))
{
   stage.removeChild(ScoreObjects[0]);
}
Eugene
I was able to remove the circle off the stage--doing stage.addChild(ScoreObjects[0]) But, I'm not sure why I'm still receiving an error in the output window. ArgumentError: Error #2025: The supplied DisplayObject must be a child of the caller. at flash.display::DisplayObjectContainer/removeChild() at Move/eatCircle()
jc70
just updated the code, this stuff is displaying, because you didn't stop your event dispatching from the box, but you already delete the object from the stage, thats why you need to check it as in my code case or another one will be just removing event handler like `box.deleteEventListener(Event.ENTER_FRAME, eatCircle);` after removing child. So thats it. Don'r forget to accept/rate answer/comments. Good luck!
Eugene