Finding a DisplayObject at any depth of the AS3 Display List

Published on
19,379 Points
1 Endorsement
Last Modified:
Sometimes you know that one object has a specific child in it, but you can't find the child.

This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar using a third party Flash class, and I knew the name of the toolbars and the buttons, but I couldn't access the button.  No syntax I tried would let me get to it.  I tried myToolbar.myButton, myToolbar["myButton"] and myToolbar.getChildByName( "myButton" ), but none of them gave me the button.  getChildByName returned null, which I knew meant that myButton wasn't a child of myToolbar.  Iterating through all the children of myToolbar, all I found were a lot of objects named with "instance" followed by a number.

We checked the work my partner had done.  He had properly named each button, but I wasn't finding it where I thought I would.

To make a long day of research short, it eventually became obvious that the button wasn't an immediate child of the toolbar.  The "instance" names seem to be pretty common to Flash; they are generated frequently in objects designed in Flash.

This question on EE got me pointed in the right direction.  I used the code from ugeb to dump the display lists of myToolbar.  The third-party toolbar software apparently added the button three levels deep.  We started doing a horrible thing - hard-coding to the "instance" variables.  This produced some ugly code:

var i117:DisplayObjectContainer = myToolbar.getChildByName( "instance117" ) as DisplayObjectContainer;
var i189:DisplayObjectContainer = i117.getChildByName( "instance189" ) as DisplayObjectContainer;
var i288:DisplayOjbectContainer = i189.getChildByName( "instance288" ) as DisplayObjectContainer;
var myButton:DisplayObject = i288.getChildByName( "i288" ) as DisplayObject;

Open in new window

This worked on my machine, but is only acceptable if you never add any more children to the display list.  This is not robust, and therefore not good.  I realized that I could modify the code that traces each child in the display list to simply find the child you want.  The code below shows the function I used:

function findChild( dispobj:DisplayObjectContainer, childname:String ):DisplayObject
    if (dispobj == null)
        dispobj = this;
    for (var j:int = 0; j < dispobj.numChildren; ++j)
        var obj:DisplayObject = dispobj.getChildAt( j ) as DisplayObject;
        if (obj.name == childname)
            return obj;
        if (obj is DisplayObjectContainer)
            var doc:DisplayObjectContainer = obj as DisplayObjectContainer;
            if (doc.numChildren > 0)
                var ret:DisplayObject = findChild( doc, childname );
                if (ret != null)
                    return ret;
    return null;

Open in new window

Usage is simple, e.g.
var myButton:DisplayObject = findChild( myToolbar, "myButton" );

This will run through every child of myToolbar, looking for one named "myButton".  If it doesn't find any, it will return null.
If you want to search the entire displaylist, simply pass null as the first argument.

By using this code, I made my code much more robust, and got the buttons hooked up easily.  Any time you can't find a DisplayObject where it should be, you can use this to find it for you.

Featured Post

Get 10% Off Your First Squarespace Website

Ready to showcase your work, publish content or promote your business online? With Squarespace’s award-winning templates and 24/7 customer service, getting started is simple. Head to Squarespace.com and use offer code ‘EXPERTS’ to get 10% off your first purchase.

Join & Write a Comment

The goal of the tutorial is to teach the user how to how to record live broadcast.
The goal of the tutorial is to teach the user how to use the auto adjust feature and what the different options do. When your video is not working right you can choose the auto adjust feature to help choose your settings.
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month