ActionScript 3.0 Design Patterns

(Chris Devlin) #1

238 | Chapter 6: Composite Pattern


Even though our airplane is a simple line drawing (to reduce complexity), it’s possi-


ble to create the airplane components using high fidelity graphic images loaded from


external files.


Building the Composite Structure


Figure 6-9 shows the hierarchical composite structure of the airplane. The fuselage


and main wings are composite nodes. The fuselage contains two main wings and two


tail wings. Each main wing contains an engine.


Example 6-25 shows theMainclass that builds the composite airplane structure. This


should be specified as thedocument classof the Flash document. The plane is dis-


played in top-down view, as shown in Figure 6-8. The build procedure is very


straightforward. Each component is instantiated and positioned relative to its parent


by assigning values to itsxandyparameters. The component is then added to its


parent composite node using theaddChild( )method. TheairPlanevariable refer-


Example 6-23. TailWing.as


package
{
public class TailWing extends Component
{
public function TailWing(weight:Number, damage:Number = 0)
{
graphics.lineStyle(20, 0x999999);
graphics.moveTo(0, 0);
graphics.lineTo(30, 0);
super(weight, damage);
}
}
}


Example 6-24. Engine.as


package
{
public class Engine extends Component
{
public function Engine(weight:Number, damage:Number = 0)
{
graphics.lineStyle(20, 0x666666);
graphics.moveTo(0, 0);
graphics.lineTo(0, 30);
graphics.lineStyle(5, 0x000000);
graphics.moveTo(-20, -12);
graphics.lineTo(20, -12);
super(weight, damage);
}
}
}

Free download pdf