Game Development Reference
Changing the stacking order
You might have noticed that the cat appears behind the frog and
that its ears appear behind the platforms, as shown in Figure 9-21.
In this game, it will appear more natural for the cat to appear above
these objects. You can add a simple line of code to fix it.
When objects are added to the stage by dragging them from the
Library or by using ]``?deh`, AS3.0 adds them to something called
the display list , which is just a list of all the objects on the stage.
The first objects on the list appear behind objects that are added
after them. Because the cat was one of the first objects added to
the stage in this game, the platforms and the frog appear in front
To make the cat appear in front of all the other objects, it
needs to be the last item on the display list. The number of
objects on the stage is stored in a built-in property called
jqi?deh`naj. If you add the following directive in the constructor
method, it will actually tell you the number of objects on the stage:
Figure 9-21. The cat is one of the
first objects on the display list,
so it appears behind the other
This displays -1 for me at this point of the project. There are 15 objects, but the display list starts num-
bering objects from ,, which means that the last object on the list is actually object number -0.
You can find out the number of any object on the list by using another built-in method called cap?deh`=p.
I know that my _]p object happens to be number / on the list (which actually means it's the fourth object,
if you start counting from zero). I can use a method called cap?deh`=p to make sure:
In my game, this displays as follows:
Lh]uan[Lh]pbkni is the class that the cat belongs to. You can append the j]ia property to cap?deh`=p
to find out the object's actual instance name. For example, cap?deh`=p$/%*j]ia has the value of _]p.