function removeChildrenOf(mc:MovieClip):void{
if(mc_mc.numChildren!=0){
var k:int = mc.numChildren;
while( k -- )
{
mc.removeChildAt( k );
}
}
}
AS3: remove All Children in a DisplayObject
13 Responses to “AS3: remove All Children in a DisplayObject”
Leave a Reply
while(mc.numChildren){
mc.removeChildAt(0);
}
while(mc.numChildren != 0) mc.removeChildAt(0);
–
This will do the trick!
try this
public static function removeAllChildren ( target:DisplayObjectContainer ) : void
{
while( target.numChildren )
target.removeChildAt( 0 );
}
You can do that with a sorter version:
while(mc.numChilren > 0)
mc.removeChildAt(0);
This would do it too:
function removeChildrenOf(docTarget:DisplayObjectContainer):void {
while(docTarget.numChildren) {
docTarget.removeChildAt(0)
}
}
I wonder if there is any difference in execution speed..
wow, thanks for this everyone.
I bet nothing better than this (it is fastest, less code, robust )
while(mc.numChildren) {
mc.removeChildAt(0)
}
Hello,
I think that it would be better to do as follows, so that the container does not recalculte its children ‘s index every time a child is removed
while(mc.numChildren)
{
mc.removeChildAt(mc.numChildren – 1)
}
Sorry, i think, this code is better, else the container will have one remaining child :
while(mc.numChildren => 0)
{
mc.removeChildAt(mc.numChildren – 1)
}
while(mc.numChildren => 0)
mc.removeChildAt(mc.numChildren – 1)
with the code above, doesn’t it still leave out some child containers from the movieclip.. for example, let’s say you are loading something, it finished and now you are removing the loader from the stage, using the method above will keep several objects from going to garbage collection, even if you declare the loader to null…. any ideas on how to clean that more efficently…
Hi Isaac, yes you’re right. The code above should remove all the children from the stage. However, if there are timers or sounds running in the children, then they will not be collected for GC.
What you can do instead is check if there are sound/timer objects playing in those children, stop them, and then remove the child from the parent. This generally happens when you use code in the timeline.
Also use this..
private function removeChildrenOf(container:Sprite):void {
while (container.numChildren != 0) {
container.removeChildAt(0);
}
}
i am try this.
it’s work
if(this.numChildren!=0){
var k:int = this.numChildren;
while( k — )
{
child = this.getChildAt(k);
if (child)
{
if (child.parent)
{
child.parent.removeChild(child);
child = null;
}
}
}
}