<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Finding a DisplayObject at any depth of the AS3 Display List

Published on
19,687 Points
13,087 Views
1 Endorsement
Last Modified:
Approved
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.
1
Comment
Author:Carnou
0 Comments

Featured Post

Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

Join & Write a Comment

In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
In this tutorial viewers will learn how to create a basic shape tween animation in Flash including shape hints for smooth animation Open a new document in Flash: Draw a shape: Select another frame (how long you want the tween to be): Right click and…
Other articles by this author

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month