Question

If im usin function to add a mc to the stage like so:

var myChild:MC= new MC();

function somefunc()
{
   stage.addChild(myMC)
}

but when I try to remove the mc by:

stage.removeChild(myMC)

I get The supplied DisplayObject must be a child of the caller error... any suggestions or work arounds?

Was it helpful?

Solution

You could try hiding and showing the movieClip, if possible. I think its a bit faster than removing and adding consistantly, code permitting. Keep in mind this is just a suggestion, someone smarter than me outta be able to help you out..

OTHER TIPS

Your code should work if the item is on the stage. Perhaps qualifying it with a conditional statement like so:

if (myMC.stage != null)
   stage.removeChild(myMC);

Alternatively you could use the following code but it is probably not best practice.

if (myMC.parent != null)    
   myMC.parent.removeChild(myMC);

The problem is not with removeChild. It's with the displaylist. If you check the parent property of the displayobject, when you call "removeChild" it will be null.

Why does it become null could be because of lots of reasons:

  • Parent is nulled before the child.
  • The child or parent have event listeners that won't let them die.
  • The Display Object is really not the instance you're trying to remove. THIS one can be very tricky to find out. Look at the "name and parent properties" of the variable you're trying to remove while calling removeChild.

You could also use this fail safe:

if(myMC.parent) myMC.parent.removeChild(myMC);

I could fix this problem by simply removing every EventListeners I added to that object before removing it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top