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

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 6-27. MainWing.as with modifiedMainWing class<br />

1 package<br />

2 {<br />

3 import flash.events.*;<br />

4<br />

5 public class MainWing extends Composite<br />

6 {<br />

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

8 {<br />

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

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

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

12<br />

13 addEventListener(MouseEvent.CLICK, doDamage);<br />

14<br />

15 super(weight, damage);<br />

16 }<br />

17<br />

18 private function doDamage(evt:Event)<br />

19 {<br />

20 this.nDamage += 20;<br />

21 trace('Damage to this wing is now ' + this.nDamage);<br />

22 if (this.nDamage > 50)<br />

23 {<br />

24 trace('Wing detached from fuselage - fatal crash!');<br />

25 parent.removeChild(this);<br />

26 }<br />

27 evt.stopPropagation( );<br />

28 // stop event propegation to subsequent nodes<br />

29 }<br />

30 }<br />

31 }<br />

Example 6-27 shows the modified MainWing class. Note the conditional statement in<br />

lines 22- 26. If the damage exceeds 50, this node removes itself from the display list<br />

(and the airplane <strong>com</strong>posite structure) by calling removeChild in its parent. Before we<br />

can test our modified <strong>com</strong>ponents, a few changes to the Main class are necessary to<br />

display total damage.<br />

Calculating Total Damage to the Airplane<br />

In the interests of keeping the example simple, we will register a mouse click handler<br />

called showDamage to the stage to display the total damage to the airplane. The following<br />

statement needs to be added at the end of the constructor in the Main class shown<br />

in Example 6-25.<br />

stage.addEventListener(MouseEvent.CLICK, showDamage);<br />

In addition, the following showDamage event handler method should be added to the<br />

Main class as well.<br />

242 | Chapter 6: Composite Pattern

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

Saved successfully!

Ooh no, something went wrong!