Jan
05
2009

Example of a recursive function to trace the display objects

To use this function, just pass your display object or stage reference to get the list of the all display objects sorted in a tree view.

The DisplayObjectContainer class that is used to check if an object has any children is an abstract base class for all objects that can contain child objects.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
import flash.display.*;
 
public class ChildList extends MovieClip
{
public function ChildList()
{
showDisplayList(stage);
}
 
function showDisplayList(object:DisplayObjectContainer, string:String = ""):void
{
for (var i:int = 0; i < object.numChildren; i++)
{
trace(string + object.getChildAt(i).name);
//the condition in the line below checks if object has any more children
if (object.getChildAt(i) is DisplayObjectContainer)
showDisplayList(DisplayObjectContainer(object.getChildAt(i)),
string + " ");

}
}
}

 

Comments 

 
0 #2 Rohan 2012-03-24 01:48
I seem to need a similar solution but can't figure it out just yet,

http://stackoverflow.com/questions/9848109/make-as3-for-each-loop-dynamically-know-depth-of-iteration
Quote
 
 
0 #1 bds 2011-10-30 18:00
nice
Quote
 

AS3 Tips

Follow me on Twitter