Summary | 243
private function showDamage(evt:Event)
{
trace('Total damage: ' + airPlane.getDamage( ));
}
Clicking on the stage will display the total damage to the airplane in the output
panel. Likewise, clicking on either the fuselage or main wings will display the cur-
rent damage to each of those components. The following output is a result of several
clicks.
Damage to this wing is now 20 // clicked on right wing
Damage to this wing is now 40 // clicked on right wing
Damage to the fuselage is now 10 // clicked on fuselage
Damage to the fuselage is now 20 // clicked on fuselage
Damage to this wing is now 20 // clicked on left wing
Total damage: 80 // clicked on stage
Damage to this wing is now 60 // clicked on right wing
Wing detached from fuselage - fatal crash!
The right wing was removed from the display list as its damage was more than 50.
Ideally, the wing should not disappear, but drop off. If each component had addi-
tional routines for autonomous motion, then the wing component could have been
removed from the fuselage and then added to the stage as a child, where it would fall
off due to gravity and other simulated physical effects.
Leveraging the display list in ActionScript 3.0 to develop composite structures has
many advantages because of its seamless integration with the Flash document object
model. This method is preferable only when the composite object is rigid—when all
component parts move with the larger whole. The airplane is a rigid body, even
though it consists of several components. Assigning values to thexandyparameters
of the root node will move the whole airplane. However, this method is not suitable
when components move independent of each other as they did with the snake appli-
cation. In the snake, the location of component nodes was not dependent on the
location of the root node, but on their immediate parent. Additional geometric trans-
formation would be required if its composite structure was implemented using the
display list.
Summary
The composite pattern allows you to build comple xsystems that are made up of sev-
eral smaller components. The components that make up the system may be individ-
ual components or containers that represent collections of components. The primary
advantage of the composite pattern is that it allows clients to treat both individual
components (leaf nodes) and composite components (composite nodes) the same
way through a common interface. This pattern has particular utility in ActionScript,
as the display list already implements the composite pattern, allowing developers to
easily build and manipulate complex display objects.