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-18 shows the IPlane interface. It defines two operations, getDamage( )and<br />

getWeight( ), that return the weight and damage.<br />

Example 6-18. IPlane.as<br />

package<br />

{<br />

public interface IPlane<br />

{<br />

function getDamage( ):Number;<br />

function getWeight( ):Number;<br />

}<br />

}<br />

Example 6-19 shows the <strong>com</strong>ponent class for the airplane. It extends the Shape class<br />

and implements the IPlane interface. As explained previously, Shape subclasses the<br />

DisplayObject class. The constructor takes the weight and initial damage parameters,<br />

and assigns them to the nWeight and nDamage properties. The implementation for<br />

getWeight( ) and getDamage( ) is to simply return the requested property, as <strong>com</strong>ponents<br />

do not have children.<br />

Example 6-19. Component.as<br />

package<br />

{<br />

import flash.display.Shape;<br />

}<br />

public class Component extends Shape implements IPlane<br />

{<br />

protected var nDamage:Number;<br />

protected var nWeight:Number;<br />

}<br />

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

{<br />

this.nDamage = damage;<br />

this.nWeight = weight;<br />

}<br />

public function getDamage( ):Number<br />

{<br />

return nDamage;<br />

}<br />

public function getWeight( ):Number<br />

{<br />

return nWeight;<br />

}<br />

It is important to note that the Shape class doesn’t inherit from the InteractiveObject<br />

class. Therefore, airplane <strong>com</strong>ponent class cannot respond to user input such as<br />

mouse clicks and key presses.<br />

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

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

Saved successfully!

Ooh no, something went wrong!