Using Flash’s Built-in Composite Structure: the Display List | 233
Note how the snake is constructed using the composite pattern, by adding children
to parent nodes. Construction is very straightforward as component and composite
nodes are added the same way. The tail is added by calling the sameadd( )method as
you would a composite body segment. In addition, theupdate( )method cascades
down seamlessly through the structure to animate the snake. Here again, we don’t
need to call separate update methods or differentiate between component and com-
posite objects. Even though our simple snake had only a single kinematic chain, this
application can be extended to incorporate multiple kinematic chains and multiple
children attached to the same parent. The construction of the structure and the
update( ) call would not get any more complicated than this.
Using Flash’s Built-in Composite Structure:
the Display List
In the previous example, we extended the Spriteclass to develop composite struc-
tures that display on stage. We first add the object to the composite structure, and
subsequently add it to thedisplay listusing theaddChild( )method. What’s the dis-
play list in ActionScript 3.0 applications? The display list is a tree structure with the
stageas its root node. It consists of all the visible elements that’ll be displayed on the
stage. The display list consists of two types of objects: (1) display objects and (2) dis-
play object containers. Every element that appears on the stage is a type ofdisplay
object. In contrast,display object containersnot only have a visual representation on
the stage, they can also have other display objects, and display object containers as
children.
Close examination of the inheritance structure of theSpriteclass will show two
classes calledDisplayObject andDisplayObjectContainer in its inheritance hierarchy.
TheDisplayObjectclass consists of methods and properties that deal mainly with the
visual presentation of an object such as thexandyproperties that represent its posi-
tion. TheDisplayObjectContainerclass inherits fromDisplayObject,defines neces-
sary properties, and implements methods to handle child objects (see the
ActionScript 3.0 documentation for more detail). Some of the child handling meth-
ods implemented by theDisplayObjectContainer class are listed below.
addChild(child:DisplayObject):DisplayObject;
getChildAt(index:int):DisplayObject;
removeChild(child:DisplayObject):DisplayObject;
The display list is indeed a composite structure with the DisplayObject and
DisplayObjectContainer representing the component and composite classes.
BothDisplayObjectandDisplayObjectContainerare abstract classes and cannot be
instantiated directly. We have to either extend these classes to define unique compo-
nents or use the classes that inherit from them. TheShapeandBitmapclasses extend