Handy AS3 destroy utility courtesy of DDB

/**
* Handy destroy utility courtesy of ddb.
*
* @param    p_targets An array of DisplayObjectContainers
* @param    doRecursion If <code>true</code>, function recurses through all children and removes their children before removing the parent.
* @param    p_c Counter used only for internal recursions.  Ignore.
*
* @return an integer representing the total remove count
*/
protected function removeAllChildren(p_targets:Array, doRecursion:Boolean = false, p_c:int = 0):int {
// //trace(“removeAllChildren()”);
var c:int;
var t:int;
var i:int;
var target:DisplayObjectContainer;
var child:DisplayObject;
// use param’s c if passed
if (p_c > 0) {
c = p_c;
}// loop through all targets
for (t = 0; t < p_targets.length; t++) {
target = p_targets[t];
// crawl through children starting at numChildren/length, ending at 0
i = (target.numChildren – 1);
// if there are any children
while (target.numChildren > 0) {
child = target.getChildAt(i);
// recursion
if (doRecursion && child is DisplayObjectContainer) {
// if child is not a DisplayObjectContainer,
// child.numChildren is undefined
// removeAllChildren will throw and error
c = removeAllChildren([child], true, c);
}
target.removeChild(child);
// keep count of children removed
c++;
// trace(” removedCount:”+c);
// decrement
i–;
}
}
return c;
}

Advertisements

No comments yet

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: