ActionScript 3.0 Design Patterns

(Chris Devlin) #1

186 | Chapter 5: Adapter Pattern


The Existing Class


TheLegacyCarclass shown in Example 5-9 is a subclass of theSpriteclass, and is


saved asLegacyCar.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); // black color
37 graphics.drawRect(xLoc - 4, yLoc - 2, 8, 4);
38 graphics.endFill( );
39 }
40
41
42 // method to set the x and y location of the sprite
43 private function setLoc(xLoc:int, yLoc:int):void
44 {
45 this.x = xLoc;
46 this.y = yLoc;
Free download pdf