10.12.2012 Views

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

ActionScript 3.0 Design Patterns.pdf - VideoTutorials-bg.com

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Example 5-11. CarAdapter.as (continued)<br />

}<br />

}<br />

The CarAdapter class implements the ICar interface and has a parameterized constructor<br />

that receives a LegacyCar instance. It contains four public methods as specified<br />

in the interface. All four methods use one of the public methods in the LegacyCar<br />

class for implementation. The steering angle is set in discrete steps of 50 degrees, as<br />

discussed previously. Note the nSteeringAngle property that keeps track of the current<br />

steering angle of the car. Adapter classes can have their own properties and<br />

methods to support implementation.<br />

The Client<br />

public function CarAdapter(car:LegacyCar)<br />

{<br />

this.legacyCar = car;<br />

this.nSteeringAngle = 0;<br />

this.goStraight( );<br />

}<br />

public function start( ):void<br />

{<br />

legacyCar.start( );<br />

}<br />

public function turnMoreToTheLeft( ):void<br />

{<br />

legacyCar.setSteeringWheelAngle(nSteeringAngle -= 50);<br />

}<br />

public function turnMoreToTheRight( ):void<br />

{<br />

legacyCar.setSteeringWheelAngle(nSteeringAngle += 50);<br />

}<br />

public function goStraight( ):void<br />

{<br />

legacyCar.setSteeringWheelAngle(nSteeringAngle = 0);<br />

}<br />

All that’s left to ac<strong>com</strong>plish is to develop a client to test the adapter. The client<br />

(Main.as shown in Example 5-12) creates a new instance of LegacyCar and passes<br />

two parameters to the constructor that places it on the center of the stage. Next, it<br />

creates an instance of CarAdapter and passes the previously created LegacyCar<br />

instance to it. The client also has to receive input from the keyboard to steer the car.<br />

To do this, the client attaches the onKeyPress( ) listener function to respond to key<br />

down events. This listener function calls appropriate methods on the adapter class<br />

based on keyboard input. The onKeyPress( ) listener function responds to the left,<br />

Example: Car Steering Adapter | 191

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!