AS3 Snippet #3: Remove all childs
I code snippet I tend to use quite often. And one that I tend to forget how I wrote the last time i used it.
while (this.numChildren > 0)
{
removeChild(this.getChildAt(0));
}
Update:
And when you want to remove a child as a part of an event handler, do this:
event.target.parent.removeChild(event.target);
Let’s say you want to delete the button you just clicked. The button dispatches an event, and this line of code goes in the event handler. event.target is the button (or what ever object you clicked). It does look at bit strange, because you have to tell the button that it’s parent should delete the button. The button can’t delete itself.
Note:
If you’re working with Flex, use this.removeAllChildren(); Thank you Bjørnar