User Guide
170 Display Programming
You can use the getChildAt() method to verify the layer order of the display objects. The
getChildAt() method returns child objects of a container based on the index number you
pass it. For example, the following code reveals names of display objects at different positions
in the child list of the
container DisplayObjectContainer object:
trace(container.getChildAt(0).name); // ball_A
trace(container.getChildAt(1).name); // ball_C
trace(container.getChildAt(2).name); // ball_B
If you remove a display object from the parent container’s child list, the higher elements on
the list each move down a position in the child index. For example, continuing with the
previous code, the following code shows how the display object that was at position 2 in the
container DisplayObjectContainer moves to position 1 if a display object that is lower in
the child list is removed:
container.removeChild(ball_C);
trace(container.getChildAt(0).name); // ball_A
trace(container.getChildAt(1).name); // ball_B
The removeChild() and removeChildAt() methods do not delete a display object instance
entirely. They simply remove it from the child list of the container. The instance can still be
referenced by another variable. (Use the
delete operator to completely remove an object.)
Because a display object has only one parent container, you can add an instance of a display
object to only one display object container. For example, the following code shows that the
display object
tf1 can exist in only one container (in this case, a Sprite, which extends the
DisplayObjectContainer class):
tf1:TextField = new TextField();
tf2:TextField = new TextField();
tf1.name = "text 1";
tf2.name = "text 2";
container1:Sprite = new Sprite();
container2:Sprite = new Sprite();
container1.addChild(tf1);
container1.addChild(tf2);
container2.addChild(tf1);
trace(container1.numChildren); // 1
trace(container1.getChildAt(0).name); // text 2
trace(container2.numChildren); // 1
trace(container2.getChildAt(0).name); // text 1
If you add a display object that is contained in one display object container to another display
object container, it is removed from the first display object container’s child list.