Game Development Reference
In-Depth Information
additional utility class called Time, which will prove handy both
here and elsewhere in later examples. The files for this example are
in the Chapter 11 folder: DrivingSim and the drivingsim package.
The end result will use the arrow keys to steer, accelerate, and
reverse and the space bar to do a hard-brake stop.
The Vehicle Class
This class will define all the basic properties of the car we
ll see on
screen. It starts with a number of constant and variable
static public const maxAcceleration:Number = 100;
static public const maxSpeed:Number = 350;
static public const maxSteering:Number = Math.PI / 40;
static public const accelerationRate:Number = 50;
static public const handBrakeFriction:Number = .75;
static public const stoppingThreshold:Number = 0.1;
The first values set are the maximum acceleration and speed per
second, followed by the maximum turning radius. Adjusting these
three values yields a very different experience, and you could easily
make them instance variables instead of static constants, thereby
allowing different cars to have different behavior. We also define the
rate of acceleration, meaning how many units we can increase our
acceleration per second. Next we set the amount of friction the hand
brake applies to the speed of the car. In this case, as long as the
hand brake is being held, the car will slow to 75% of its current
speed. The last constant is called the stoppingThreshold , which is the
value below which the game will round the speed down to 0. This is
present because when multiplying a number between 0 and 1, the
result will gradually get closer to 0, but never reach it.
protected var _speed:Number = 0; //PIXELS PER SECOND
protected var _acceleration:Number = 0; //PIXELS PER SECOND
protected var _angle:Number = 0; //ANGLE IN RADIANS
Next come three protected variables for speed, acceleration, and
the angle of the car, all initially set to 0. Out constructor is empty
for this example, so we
ll skip it and move on to the three getter/
setter function pairs that will complete this class.
public function get angle():Number {
return _angle;
public function set angle(value:Number):void {
_angle = value;
rotation = _angle * (180 / Math.PI);
Search Nedrilad ::

Custom Search