View Full Version : removeChild Question
skelebunny
November 25th, 2008, 07:04 PM
I've created a for loop that adds random circles to the stage when you're in frame 5. That works just fine. I've been trying for hours to get rid of the circles that the for loop creates when I goto another frame. I can't for the life of me get rid of them. THis is the code I have and it's not working for me. Any help would be GREATLY appreciated.
if(currentFrame == 5)
{
for (var i:Number = 0; i < 35; i++)
{
var Container:MovieClip = new MovieClip;
var spec:Spec = new Spec();
spec.x = Math.random() * stage.stageWidth / 2;
spec.y = Math.random() * stage.stageHeight / 2;
spec.alpha = Math.random();
addChild(Container);
Container.addChild(spec);
}
}
if(currentFrame != 5)
{
removeChild(Container.spec);
}
Rundevo
November 25th, 2008, 09:27 PM
Container.removeChild(spec);
skelebunny
November 25th, 2008, 11:49 PM
It still doesn't work. When I go to the other frame, for the some reason the specs from my for loop are still there and behind another display object. I tried putting a removeChild on the other section of the site but the spec still remains. Any other ideas?
tarkuser
November 26th, 2008, 02:06 AM
Try declaring the MovieClip at top instead of giving inside.
var Container:MovieClip = new MovieClip;
just try whether it works...
skelebunny
November 26th, 2008, 08:11 AM
No luck. Is there a different method for removing multiple instances of a MovieClip from the stage? That's probably a stupid question. I'm new.
Try declaring the MovieClip at top instead of giving inside.
var Container:MovieClip = new MovieClip;
just try whether it works...
Joony5
November 26th, 2008, 08:41 AM
while(Container.numChildren){
Container.removeChildAt(0);
}
Joony5
November 26th, 2008, 08:42 AM
The problem is that you're overwriting the "spec" object in every iteration, so if you remove "spec" you're just removing the last one.
skelebunny
November 26th, 2008, 08:49 AM
Hi Joony. Thanks for your quick reply. Am I supposed to wrap that while statement in an if statement? If so, I tried that and when I go to another frame the specs still remain. This is the new code I have.
var Container:MovieClip = new MovieClip;
if(currentFrame == 5)
{
for (var i:Number = 0; i < 35; i++)
{
var spec:Spec = new Spec();
spec.x = Math.random() * stage.stageWidth / 2;
spec.y = Math.random() * stage.stageHeight / 2;
spec.alpha = Math.random();
addChild(Container);
Container.addChild(spec);
}
}
if(currentFrame != 5){
while(Container.numChildren)
{
Container.removeChildAt(0);
}
}
Rundevo
November 26th, 2008, 09:28 AM
var count:uint = container.numChildren;
for (var i:uint = 0; i < count; i++)
{
container.removeChildAt(0);
}
How about that one? :)
/rundevo
skelebunny
November 26th, 2008, 09:44 AM
Hey Rundevo, am I suppose to put the code you suggested in an if statement? This is the new code.
var Container:MovieClip = new MovieClip;
if(currentFrame == 5)
{
for (var i:Number = 0; i < 5; i++)
{
var spec:Spec = new Spec();
spec.x = Math.random() * stage.stageWidth / 2;
spec.y = Math.random() * stage.stageHeight / 2;
spec.alpha = Math.random();
addChild(Container);
Container.addChild(spec);
}
}
if (currentFrame != 5){
var count:uint = Container.numChildren;
for (var j:uint = 0; j < count; j++)
{
Container.removeChildAt(0);
}
}
Unfortunately this code does not work either.
Rundevo
November 26th, 2008, 09:54 AM
I would use afew trace statements around the code and see what doesnt add up!
Like trace(Container.numChildren) and whether the if statements returns true and actually runs the code inside them.
Maybe put the for loop in a function instead!
Iamthejuggler
November 26th, 2008, 10:27 AM
You need to add an enter frame listener, as below.
var _Container:MovieClip = new MovieClip();
addChild(_Container);
this.addEventListener(Event.ENTER_FRAME, ef);
function ef(e:Event):void{
if(currentFrame == 5)
{
for (var i:Number = 0; i < 35; i++)
{
var spec:Spec = new Spec();
spec.x = Math.random() * stage.stageWidth / 2;
spec.y = Math.random() * stage.stageHeight / 2;
spec.alpha = 1;
_Container.addChild(spec);
}
}
if(currentFrame != 5){
while(_Container.numChildren != 0)
{
_Container.removeChildAt(0);
}
}
}
skelebunny
November 26th, 2008, 10:42 AM
Yes! It worked. I had to tinker around with it a bit but throwing it in a function was what did the trick. I really appreciate it guys.
skelebunny
November 26th, 2008, 10:43 AM
Just to clarify, I through the loop in a function like you suggested. Thanks again.
Rundevo
November 26th, 2008, 11:21 AM
No problem, glad to help when I can :)
All the best
/rundevo
Powered by vBulletin® Version 4.1.10 Copyright © 2012 vBulletin Solutions, Inc. All rights reserved.