ActionScript 3.0 Design Patterns

(Chris Devlin) #1
Using Flash’s Built-in Composite Structure: the Display List | 235

Example 6-18 shows theIPlaneinterface. It defines two operations,getDamage( )and


getWeight( ), that return the weight and damage.


Example 6-19 shows the component class for the airplane. It extends theShapeclass


and implements theIPlaneinterface. As explained previously,Shapesubclasses the


DisplayObjectclass. The constructor takes the weight and initial damage parame-


ters, and assigns them to thenWeightandnDamageproperties. The implementation for


getWeight( )andgetDamage( )is to simply return the requested property, as compo-


nents do not have children.


It is important to note that theShapeclass doesn’t inherit from theInteractiveObject


class. Therefore, airplane component class cannot respond to user input such as


mouse clicks and key presses.


Example 6-18. IPlane.as


package
{
public interface IPlane
{
function getDamage( ):Number;
function getWeight( ):Number;
}
}


Example 6-19. Component.as


package
{
import flash.display.Shape;


public class Component extends Shape implements IPlane
{
protected var nDamage:Number;
protected var nWeight:Number;


public function Component(weight:Number, damage:Number = 0)
{
this.nDamage = damage;
this.nWeight = weight;
}


public function getDamage( ):Number
{
return nDamage;
}


public function getWeight( ):Number
{
return nWeight;
}
}
}

Free download pdf