2

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!

mazName
  • 145
  • 9

3 Answers3

2

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);
 }
}
mazName
  • 145
  • 9
1

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);
  }
}
Organis
  • 7,243
  • 2
  • 12
  • 14
  • This avoid the Error but remove the [object MainTimeline] too. So there are no more children after the first loop. @Organis – tatactic Jan 03 '17 at 16:09
1

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]

Community
  • 1
  • 1
tatactic
  • 1,379
  • 1
  • 9
  • 18
  • @Maziar I would really want to know what the output panel will output in Your case. I'm probably wrong, but this is always good to test the code with some trace() function or try...catch blocks. – tatactic Jan 03 '17 at 16:30