User Guide
172 Display Programming
var url:URLRequest = new URLRequest("banana.jpg");
pict.load(url);
pict.name = "banana loader";
container.addChild(title);
container.addChild(pict);
The getChildAt() method returns the child of the display list at a specific index position:
trace(container.getChildAt(0) is TextField); // true
You can also access child objects by name. Each display object has a name property, and if you
don’t assign it, Flash Player assigns a default value, such as
"instance1". For example, the
following code shows how to use the
getChildByName() method to access a child display
object with the name
"banana loader":
trace(container.getChildByName("banana loader") is Loader); // true
Using the getChildByName() method can result in slower performance than using the
getChildAt() method.
Since a display object container can contain other display object containers as child objects in
its display list, you can traverse the full display list of the application as a tree. For example, in
the code excerpt shown earlier, once the load operation for the
pict Loader object is
complete, the
pict object will have one child display object, which is the bitmap, loaded. To
access this bitmap display object, you can write
pict.getChildAt(0). You can also write
container.getChildAt(0).getChildAt(0) (since container.getChildAt(0) == pict).
The following function provides an indented
trace() output of the display list from a display
object container:
function traceDisplayList(container:DisplayObjectContainer,
indentString:String = ""):void
{
var child:DisplayObject;
for (var i:uint=0; i < container.numChildren; i++)
{
child = container.getChildAt(i);
trace (indentString, child, child.name);
if (container.getChildAt(i) is DisplayObjectContainer)
{
traceDisplayList(DisplayObjectContainer(child), indentString + "")
}
}
}