ActionScript3 – show DisplayObject hierarchy

This simple function will output (trace) hierarchy of chosen DisplayObjectContainer when called. This is useful for looking up what's added to stage "overall" or just checking how many nodes does your movie has.
/**
 * Outputs hierarchy of children for chosen DisplayObjectContainer.
 * @param   what    container to show childs of
 * @param   c       (optional) Initial level (tab count)
 */
public function traceChildren(what:DisplayObjectContainer, c:int = 0):void {
    var j:int, child:DisplayObject, out:String;
    for (var i:int = 0, l:int = what.numChildren; i < l; i++) {
        child = what.getChildAt(i);
        // add 'tabs' before text:
        for (j = 0, out = ''; j < c; j++) out += '. ';
        // add component's toString():
        out += child.toString();
        // display output:
        trace(out);
        // if child is container too, trace its children:
        if (child is DisplayObjectContainer) {
            traceChildren(child as DisplayObjectContainer, c + 1);
        }
    }
}

Related posts:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.