O'Reilly logo

ActionScript 3.0 Design Patterns by Chandima Cumaranatunge, William Sanders

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Example: Car Steering Adapter

Adapter patterns are commonly used when there is an existing legacy class that provides the functionality you require, but whose interface doesn't conform to what you need. Let's assume that we have a legacy class called LegacyCar, which is a car sprite from an old game. The LegacyCar class was initially developed for use with a steering wheel input device to provide an authentic driving experience. We will change the interface to the LegacyCar class so that we can steer the car using different input devices such as a keyboard and mouse.

The Existing Class

The LegacyCar class shown in Example 5-9 is a subclass of the Sprite class, and is saved as LegacyCar.as.

Example 5-9. LegacyCar.as

1 package { 2 3 import flash.display.*; 4 import flash.events.*; 5 import flash.geom.*; 6 7 public class LegacyCar extends Sprite 8 { 9 10 internal var nSpeed:Number; // holds speed of car in pixels/frame 11 internal var nSteeringWheelAngle:Number; // steering rotation in Degrees 12 13 public function LegacyCar(xLoc:int, yLoc:int) 14 { 15 nSpeed = 5; 16 nSteeringWheelAngle = 0; 17 this.drawCar(); 18 this.setLoc(xLoc, yLoc); 19 } 20 21 private function drawCar():void 22 { 23 // draw car body 24 graphics.beginFill(0x00FF00); // green color 25 graphics.drawRect(-20, -10, 40, 20); 26 graphics.endFill(); 27 // draw tires 28 drawTire(-12, -15); 29 drawTire(12, -15); 30 drawTire(-12, 15); 31 drawTire(12, 15); 32 } 33 34 private function drawTire(xLoc:int, yLoc:int) 35 { 36 graphics.beginFill(0x000000); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required