Remove and null all children of a display object container


/ Published in: ActionScript 3
Save to your folder(s)

I create this recursive function to remove and null all children inside a display object container.


Copy this code and paste it in your HTML
  1. function removeAllChildren(parentChild:*):void
  2. {
  3. for(var i:uint = 0; i < parentChild.numChildren;++i)
  4. {
  5. //check if child is a DisplayObjectContainer, which could hold more children
  6. if(parentChild.getChildAt(i) is DisplayObjectContainer) removeAllChildren(DisplayObjectContainer(parentChild.getChildAt(i)));
  7. else
  8. {
  9. //remove and null child of parent
  10. var child:DisplayObject = parentChild.getChildAt(i);
  11. parentChild.removeChild(child);
  12. child = null;
  13. }
  14.  
  15. }
  16. //remove and null parent
  17. parentChild.parent.removeChild(parentChild);
  18. parentChild = null;
  19. }
  20.  
  21.  
  22. //usage with a movieclip
  23. removeAllChildren(yourMc);
  24.  
  25.  
  26. //usage with your root
  27. removeAllChildren(root);

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.