Jan
05
2009

Example of recursive function to trace the display objects

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

The DisplayObjectContainer class that is used to check if 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 + "  ");
      }
   }
}

 


AS3 Tips