In this article we will see how to remove all the children from a display object. This will be useful when we need to remove all the Movieclip or sprite from a parent Container.
Difficulty:
Basic Users
Remove all children from Display Object
In this article we are going to use the following properties of method.
- numChildren
- removeChildAt( )
numChildren in AS3
numChildren is a public method available for all DisplayObjects in as3 which gives you the count, number of child objects inside the parent object.
Suppose there are 3 sprites inside the parent MovieClip named containerMC then we can count the number of sprites inside the containerMC by
trace("Children Count: "+containerMC.numChildren)
output:
Children Count: 3
removeChildAt() in AS3
Removes the children at given depth, depth we pass as argument.
Example:
parentContainer.removeChildAt(0);
Above code removes the children at depth “0″ from parentContainer.
Note: In as3 if we remove an child object from middle of display object then the depth will be rearranged itself.
AS3 Remove all children
while( container.numChildren > 0 ) container.removeChildAt( 0 ); }
Above code removes all the children from the parent container until there is no child left.
Related posts: