Game Development Reference
In-Depth Information
if (e.keyCode == Keyboard.SPACE) _spacePressed = true;
protected function keyUp(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.LEFT) _leftPressed = false;
if (e.keyCode == Keyboard.RIGHT) _rightPressed = false;
if (e.keyCode == Keyboard.UP) _upPressed = false;
if (e.keyCode == Keyboard.DOWN) _downPressed = false;
if (e.keyCode == Keyboard.SPACE) _spacePressed = false;
If you open the FLA file associated with this example and run it,
you will see that the vehicle instance on the Stage is now controllable
with the arrow keys and space bar. This is just the foundation for a
it has no collision detection, computer AI, or even goals. One
other thing to note about this example is that the car moves like it has
the best tires ever made and can turn on a dime. While this is okay
and might work perfectly for certain scenarios, the simulation could be
a little more realistic with the addition of the ability to
the car,
essentially making the motion of the car to continue in the direction it
was previously traveling. Let
s look at how we could achieve that now.
Example: Top-Down Driving Game with Drift
In the previous example, we applied the acceleration directly to the
speed of the car without taking into account the direction of the accel-
eration. Remember how we learned that vectors have both a magni-
tude and a direction earlier in this chapter. If we set both the
acceleration and velocity of the car to vectors, we
ll gain more realistic
behavior when we combine them. Since this is just a modification of
the previous example, I won
t cover any sections of the code that
t changed. The files for this example are in the Chapter 11 folder;
the FLA is DrivingSimDrift and the associated package is called driving-
simdrift. Let
s start by looking at the changes to the Vehicle class.
static public const maxSpeed:Number = 350;
static public const maxSteering:Number = Math.PI / 30;
static public const maxAcceleration:Number = 400;
static public const handBrakeFriction:Number = .75;
static public const stoppingThreshold:Number = 0.1;
protected var _velocity:Vector3D = new Vector3D();
protected var _acceleration:Vector3D = new Vector3D();
protected var _angle:Number = 0;
Search Nedrilad ::

Custom Search