PDA

View Full Version : Removing child from stage



Petwoip
November 4th, 2009, 01:57 AM
Yeah, this is probably the most common problem on these boards. However, I think my specific problem is sufficiently different.

I have a movie clip on the stage with the instance name "ball".
In my code, I addChild(ball) to a Ball object called "mainBall".
After doing that, I want to remove "ball" from the stage.
Finally, I add addChild(mainBall) to stage.

The code takes place in my Document class:


var mainBall:MovieClip = new Ball()
mainBall.addChild(ball)
stage.removeChild(ball)
addChild(mainBall)
From the code, I get the error: "The supplied DisplayObject must be a child of the caller".

It might be hard to explain why exactly I'm going through this process. If possible, just ignore that and try to explain why what I've done doesn't work.

ggalan
November 4th, 2009, 02:38 AM
you cant call it ball now because the name is now mainBall

Maqrkk
November 4th, 2009, 04:02 AM
I think the problem is very straightforward, it's exactly what the error says it is:

mainBall.addChild(ball)
stage.removeChild(ball)

You add the ball to the mainBall, then try to remove it from the stage. It's not a direct child of the stage, it's a child of mainBall.

Felixz
November 4th, 2009, 04:54 AM
var mainBall:MovieClip = new Ball();
mainBall.addChild(ball);
mainBall.removeChild(ball);
addChild(mainBall);

Petwoip
November 4th, 2009, 10:24 AM
Thanks everyone! It works now