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.

intercept key down events, we need to attach listeners to intercept mouse down and<br />

mouse move events. Note that the listeners should be attached to the stage rather<br />

than the LegacyCar object. This enables the listener functions to receive mouse clicks<br />

over the broad area of the stage, as opposed to directly over the car object.<br />

this.stage.addEventListener(MouseEvent.MOUSE_MOVE, this.doMouseMove);<br />

this.stage.addEventListener(MouseEvent.MOUSE_DOWN, this.doMouseDown);<br />

All that’s required now is to implement the listener functions that call the appropriate<br />

methods in the adapter. The doMouseDown( ) function listens to mouse down events<br />

and calls the goStraight( ) method in the adapter.<br />

private function doMouseDown(event:MouseEvent):void<br />

{<br />

this.carAdapter.goStraight( );<br />

}<br />

The doMouseMove( ) function listens to mouse move events. It checks whether the<br />

mouse has moved to the left or right from its last position. The last mouse position is<br />

saved in the oldMouseStageX property. Based on whether the mouse moved to the left<br />

or right, the adapter methods turnMoreToTheLeft( ) and turnMoreToTheRight( ) are<br />

called.<br />

private function doMouseMove(event:MouseEvent):void<br />

{<br />

// change in horizontal mouse position<br />

var xDiff = event.stageX - this.oldMouseStageX;<br />

if (xDiff > 0) {<br />

this.carAdapter.turnMoreToTheRight( );<br />

} else {<br />

this.carAdapter.turnMoreToTheLeft( );<br />

}<br />

event.updateAfterEvent( ); // process this event first<br />

this.oldMouseStageX = event.stageX // save old mouse x position<br />

}<br />

As is evident from this example, that well-thought-out design of adapters can be used<br />

in multiple contexts.<br />

Example: List Display Adapter<br />

Adapter patterns also <strong>com</strong>e in handy to create reusable classes that use existing<br />

classes to implement some of the required functionality. For example, think about<br />

when you would want to display a list of values on the Stage. There may be many<br />

instances when you want to do this: lists of names, lists of high scores in a game, lists<br />

of products in a shopping cart, etc. In Flash you can use a TextField to display a list,<br />

putting each item on a separate line. However, this requires that the display text be<br />

preformatted with a carriage return character ('\r', ASCII 13) separating each list<br />

item before sending it out to the TextField object for display. The following code<br />

snippet will display a list using a TextField object.<br />

194 | Chapter 5: Adapter Pattern

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

Saved successfully!

Ooh no, something went wrong!