ActionScript 3.0 Design Patterns

(Chris Devlin) #1

188 | Chapter 5: Adapter Pattern


drawTire( )method (lines 34-39). The car is drawn so that itsregistration pointis in


the center of the sprite.


The LegacyCarclass has two public methods. Thestart( )method (lines 50-59)


checks whether the sprite has been added to thedisplay listby checking the existence


of thestageobject (a sprite can be added to the display list using theaddChild( )


method). If thestageobject exists, it registers thedoMoveCar( )listener method to


respond toenter frameevents (line 55). ThedoMoveCar( )method essentially gets the


car moving forward bynSpeedpixels per frame. TheLegacyCarclass was not designed


to control the speed of the car or stop it from moving.


The other public method (lines 62-65) issetSteeringWheelAngle( ), which takes the


angle of the steering wheel in degrees as a parameter, and sets the


nSteeringWheelAngleproperty to it. ThenSteeringWheelAngleproperty represents the


rotation angle of the steering wheel in degrees.


ThedoMoveCar( )method (lines 68-83) is theenter frameevent handler (it was regis-


tered as the enter fame handler in line 55). An enter fame event is generated each


time the playhead in Flash enters a new frame. The frame rate can be set within the


Flash document. ThedoMoveCar( )method is executed on each enter frame event. It


rotates the car sprite by 1 percent of the rotation angle of the steering wheel (stored


innSteeringWheelAngle). For example, if the rotation angle of the steering wheel is


negative 200 degrees, the car sprite will rotate 2 degrees anticlockwise each time an


enter frameevent is received. It then uses thepolar( )method in thePointclass to


figure out the next location of the car sprite, based on its speed (nSpeed) and current


direction (rotationproperty of theSpriteclass). It then moves the sprite to its new


location. The final task of thedoMoveCar( )function is to check whether the car sprite


has moved offscreen. If so, it simply moves it back to the center of the stage.


TheLegacyCarclass is not the most functional car we’ve come across, but it provides


a solid implementation for steering a car using a steering wheel input device. We


hope to make use of this in our new context that requires us to steer the car using


different input devices.


Interface Conversion


The primary function of an adapter pattern is to convert the interface of an existing


class to fit into a new context. First, we need to figure out the original interface, and


then determine the requirements of the new context. After this, we can determine if


converting the interface of the existing class using an adapter is a viable option.


The original interface


TheLegacyCarclass has two public methods that represent its interface. Public meth-


ods are the primary way by which a client can access and manipulate an object. The


publicstart( )method simply gets the car moving. Therefore, it functions like the

Free download pdf