AS3: remove All Children in a DisplayObject

function removeChildrenOf(mc:MovieClip):void{
	if(mc_mc.numChildren!=0){
		var k:int = mc.numChildren;
		while( k -- )
		{
			mc.removeChildAt( k );
		}
	}
}

13 Responses to “AS3: remove All Children in a DisplayObject”

  1. flashyMcGee

    while(mc.numChildren){
    mc.removeChildAt(0);
    }

  2. Martin Sandström

    try this

    public static function removeAllChildren ( target:DisplayObjectContainer ) : void
    {
    while( target.numChildren )
    target.removeChildAt( 0 );
    }

  3. Mem's

    You can do that with a sorter version:

    while(mc.numChilren > 0)
    mc.removeChildAt(0);

  4. josefk

    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..

  5. Jasmeet

    I bet nothing better than this (it is fastest, less code, robust )

    while(mc.numChildren) {
    mc.removeChildAt(0)
    }

  6. ran

    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)
    }

  7. ran

    Sorry, i think, this code is better, else the container will have one remaining child :

    while(mc.numChildren => 0)
    {
    mc.removeChildAt(mc.numChildren – 1)
    }

  8. isaac ewing

    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…

  9. Edwin

    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.

  10. firos

    Also use this..
    private function removeChildrenOf(container:Sprite):void {
    while (container.numChildren != 0) {
    container.removeChildAt(0);
    }
    }

  11. sanjay Ramani

    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;
    }
    }
    }
    }

Leave a Reply

XHTML: You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>