How to remove all MovieClips, Sprites and graphics from a scene in AS3?

368 views Asked by At

I have a scene including all MovieClips, Sprites, graphics that I bring some of them to the stage using addChild(...).

I want to remove all of them because I can still see them when I go to other scenes.

I have used below code but it shows me the error mentioned below:

btn.addEventListener(MouseEvent.CLICK,removing);

function removing(e:MouseEvent):void
{
  while (stage.numChildren > 0)
  {
    stage.removeChildAt(0);
  }

}

Error:

TypeError: Error #1009: Cannot access a property or method of a null object reference. at Show_fla::MainTimeline/removing()

Thanks in advance for your time and help!

3

There are 3 answers

0
mazName On BEST ANSWER

As it shows, it is not working with while loop and it is working with for loop|:

btn.addEventListener(MouseEvent.CLICK,removing);

function removing(e:MouseEvent):void
{
 var i:int = 0;
 for (i=stage.numChildren-1; i>=0; i--)
 {
    stage.removeChildAt(i);
 }
}
1
Organis On

The property DiaplayObject.stage is defined ONLY while the given DisplayObject is actually attached to stage. As soon as you remove the Sprite/MovieClip that holds the removing code, its .stage changes to null and the next condition check stage.numChildren naturally fails. You should keep a reference to stage in a local variable.

btn.addEventListener(MouseEvent.CLICK,removing);

function removing(e:MouseEvent):void
{
  var aStage:Stage = stage;

  while (aStage.numChildren > 0)
  {
    aStage.removeChildAt(0);
  }
}
1
tatactic On

If You add a trace of the object that you are removing, You will see that You remove the [object MainTimeline], so You don't even need a loop.

In Your code You remove the [object MainTimeline] and all clips are deleted. In the while loop it throws an Error not in the for loop.

function removing(e:MouseEvent):void {
    var i:int = 0;
    for (i=stage.numChildren-1; i>=0; i--)
    {
        trace("removing : " + (stage.getChildAt(i)));
        stage.removeChildAt(i);
    }
}

Output :

removing : [object MainTimeline]

So You remove the Object [object MainTimeline] and have no more children to remove.

function removing(e:MouseEvent):void {
    trace("removing : " + (stage.getChildAt(0)));
    stage.removeChildAt(0);
}

Will probably give You the same output:

removing : [object MainTimeline]

So You don't even need a loop if the [object MainTimeline] is removed.

I didn't test it in the same conditions, so please tell us if You have the same output.

I suggest you to check the answer from @LukeVanIn that explains the difference between stage, root and main timeline

[EDIT]

function removingWhile(e:MouseEvent):void {
    while (stage.numChildren > 0){
    count++;
    trace("removing : " + (stage.getChildAt(0)));
    trace ("number of iterations = " + (count++).toString())
    stage.removeChildAt(0);
  }
}

Will output :

removing : [object MainTimeline] number of iterations = 1

TypeError: Error #1009... at Untitled_fla::MainTimeline/removingWhile()

[/EDIT]