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.

Example 6-23. TailWing.as<br />

package<br />

{<br />

public class TailWing extends Component<br />

{<br />

public function TailWing(weight:Number, damage:Number = 0)<br />

{<br />

graphics.lineStyle(20, 0x999999);<br />

graphics.moveTo(0, 0);<br />

graphics.lineTo(30, 0);<br />

super(weight, damage);<br />

}<br />

}<br />

}<br />

Example 6-24. Engine.as<br />

package<br />

{<br />

public class Engine extends Component<br />

{<br />

public function Engine(weight:Number, damage:Number = 0)<br />

{<br />

graphics.lineStyle(20, 0x666666);<br />

graphics.moveTo(0, 0);<br />

graphics.lineTo(0, 30);<br />

graphics.lineStyle(5, 0x000000);<br />

graphics.moveTo(-20, -12);<br />

graphics.lineTo(20, -12);<br />

super(weight, damage);<br />

}<br />

}<br />

}<br />

Even though our airplane is a simple line drawing (to reduce <strong>com</strong>plexity), it’s possible<br />

to create the airplane <strong>com</strong>ponents using high fidelity graphic images loaded from<br />

external files.<br />

Building the Composite Structure<br />

Figure 6-9 shows the hierarchical <strong>com</strong>posite structure of the airplane. The fuselage<br />

and main wings are <strong>com</strong>posite nodes. The fuselage contains two main wings and two<br />

tail wings. Each main wing contains an engine.<br />

Example 6-25 shows the Main class that builds the <strong>com</strong>posite airplane structure. This<br />

should be specified as the document class of the Flash document. The plane is displayed<br />

in top-down view, as shown in Figure 6-8. The build procedure is very<br />

straightforward. Each <strong>com</strong>ponent is instantiated and positioned relative to its parent<br />

by assigning values to its x and y parameters. The <strong>com</strong>ponent is then added to its<br />

parent <strong>com</strong>posite node using the addChild( ) method. The airPlane variable refer-<br />

238 | Chapter 6: Composite Pattern

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

Saved successfully!

Ooh no, something went wrong!