Using Flash’s Built-in Composite Structure: the Display List | 237
Unlike theShapeclass,Spritedoes inherit fromInteractiveObject. Therefore, com-
posite components in the airplane can respond to user interface events. We can now
develop the component and composite classes that make up the airplane.
Creating the Fuselage, Wings, and Engines
Now that theComponentandCompositeclasses have been developed, we can create
the nodes that make up the airplane. Example 6-21 through Example 6-24 show the
Fuselage,MainWing,TailWing, andEngineclasses. TheFuselageandMainWingclasses
represent composite nodes that hold other components and extend theComposite
class (see Example 6-20). TheTailWingandEngineclasses are leaf nodes and extend
theComponentclass (see Example 6-19). The implementation of all these subclasses is
very similar to each other. The component is drawn with simple lines, using the
graphics property of theDisplayObjectclass. The first parameter of the constructor
method is the weight, and the second is the initial damage (defaults to zero). Note
the call to the superclass constructor using thesuperkeyword in the last line of the
constructor. This ensures proper initialization of properties defined in the superclass.
Example 6-21. Fuselage.as
package
{
import flash.events.*;
public class Fuselage extends Composite
{
public function Fuselage(weight:Number, damage:Number = 0)
{
graphics.lineStyle(40, 0xC0C0C0);
graphics.moveTo(0, 0);
graphics.lineTo(0, 150);
super(weight, damage);
}
}
}
Example 6-22. MainWing.as
package
{
public class MainWing extends Composite
{
public function MainWing(weight:Number, damage:Number = 0)
{
graphics.lineStyle(25, 0x999999);
graphics.moveTo(0, 0);
graphics.lineTo(100, 0);
super(weight, damage);
}
}
}