10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Note how the snake is constructed using the <strong>com</strong>posite pattern, by adding children<br />

to parent nodes. Construction is very straightforward as <strong>com</strong>ponent and <strong>com</strong>posite<br />

nodes are added the same way. The tail is added by calling the same add( ) method as<br />

you would a <strong>com</strong>posite body segment. In addition, the update( ) method cascades<br />

down seamlessly through the structure to animate the snake. Here again, we don’t<br />

need to call separate update methods or differentiate between <strong>com</strong>ponent and <strong>com</strong>posite<br />

objects. Even though our simple snake had only a single kinematic chain, this<br />

application can be extended to incorporate multiple kinematic chains and multiple<br />

children attached to the same parent. The construction of the structure and the<br />

update( ) call would not get any more <strong>com</strong>plicated than this.<br />

Using Flash’s Built-in Composite Structure:<br />

the Display List<br />

In the previous example, we extended the Sprite class to develop <strong>com</strong>posite structures<br />

that display on stage. We first add the object to the <strong>com</strong>posite structure, and<br />

subsequently add it to the display list using the addChild( ) method. What’s the display<br />

list in <strong>ActionScript</strong> <strong>3.0</strong> applications? The display list is a tree structure with the<br />

stage as its root node. It consists of all the visible elements that’ll be displayed on the<br />

stage. The display list consists of two types of objects: (1) display objects and (2) display<br />

object containers. Every element that appears on the stage is a type of display<br />

object. In contrast, display object containers not only have a visual representation on<br />

the stage, they can also have other display objects, and display object containers as<br />

children.<br />

Close examination of the inheritance structure of the Sprite class will show two<br />

classes called DisplayObject and DisplayObjectContainer in its inheritance hierarchy.<br />

The DisplayObject class consists of methods and properties that deal mainly with the<br />

visual presentation of an object such as the x and y properties that represent its position.<br />

The DisplayObjectContainer class inherits from DisplayObject, defines necessary<br />

properties, and implements methods to handle child objects (see the<br />

<strong>ActionScript</strong> <strong>3.0</strong> documentation for more detail). Some of the child handling methods<br />

implemented by the DisplayObjectContainer class are listed below.<br />

addChild(child:DisplayObject):DisplayObject;<br />

getChildAt(index:int):DisplayObject;<br />

removeChild(child:DisplayObject):DisplayObject;<br />

The display list is indeed a <strong>com</strong>posite structure with the DisplayObject and<br />

DisplayObjectContainer representing the <strong>com</strong>ponent and <strong>com</strong>posite classes.<br />

Both DisplayObject and DisplayObjectContainer are abstract classes and cannot be<br />

instantiated directly. We have to either extend these classes to define unique <strong>com</strong>ponents<br />

or use the classes that inherit from them. The Shape and Bitmap classes extend<br />

Using Flash’s Built-in Composite Structure: the Display List | 233

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!