flash display_list_depth_change
Depth change
In certain situations we can have the need to inverse the stacking order of two graphic objects. To do this we use the methods swapChildren or swapChildrenAt. We will look at the first one’s signeture here:
public function swapChildren(child1:DisplayObject,child2:DisplayObject):void
The swapChildren method accepts two parameters of the DisplayObject type. Contrary to the setChildIndex method, the swapChildren and swapChildrenAt methods don’t change the index of other graphic objects when the depth is changed.
Let’s call the swapChildren method on our main timeline which contains our two graphic objects. The following code precedes the depth change:
swapChildren ( myCircle, myRectangle );
If we test our animation we get the following display:
A successive call of the swapChildren method inverts the two DisplayObjects on each call. If we add a call to the swapChildren method we get the original display. We add a second call to the swapChildren method on our layer:
swapChildren ( myCircle, myRectangle ); swapChildren ( myCircle, myRectangle );
We get our original organisation:
When we have no reference to the DisplayObjects to invert we can use the swapChildrenAt method. This accepts the two indexes as parameters:
public function swapChildrenAt(index1:int, index2:int):void
If the index is negative or doesn’t exist in the child list, the call for the method will raise a RangeError type exception. We can therefore arrive at our same result as our previous example without specifying the occurrence name but the index of the DisplayObjects:
swapChildrenAt ( 1, 0 );
If we only had one reference and an index we could obtain the same result by using different methods than we have looked at:
swapChildrenAt ( getChildIndex (myRectangle), 0 );
Or another, if the interest is less justified:
swapChildren ( getChildAt (1), getChildAt (0) );
Worth remembering
- To change the stacking order of the DisplayObjects in the child object list, we use the setChildIndex method
- The setChildIndex method pushes the other graphic objects either upwards or downwards from one index to another from the child list.
- To invert the stacking order of two DisplayObjects the swapChildren and swapChildrenAt methods are used.
- The swapChildren and swapChildrenAt methods do not modify the stacking order of other graphic objects in the child object list.
Mediabox Training Centre © 2000 - 2008 All rights reserved.
Adobe Authorized Training Centre. State convention under number 25 14 02167 14.
Mediabox : SARL au capital de 62.000€ - Activity number: 25 14 02167 14 - SIRET : 493 716 468 00027
MEDIABOX, 102 Avenue des Champs Elysées, 75008 PARIS - Tel. +33(0)2.31.91.96.89 - Fax. +33(0)2.72.68.56.42




