Lets start this off with the example from the book (if you own the book
ActionScript 3.0 Game Programming University you can get files
here)
You may notice several things that just don't feel right with it, but for today I'm just going to look at one, that being there's no acceleration for the vehicle starting/stopping.
So first off, we're going to have to find the speed in the actionscript, it'll look something like this:
static const speed:Number = .3;
we're going to want to change this to a variable, and since speed is going to be continuously applied instead of just when there's user input we're gonna change it to 0(at rest):
private var speed:Number = 0;
Now we need to add the variables needed to apply the acceleration, so I'm going to throw in the acceleration variable, a max speed (just higher than the old one so it'll be a bit faster ;)) and a friction variable which will slow the vehicle down when the accelerator isn't being applied:
static const acceleration:Number = 0.002;
static const friction:Number = 0.0001;
static const maxSpeed:Number = .4;
So now with your variables set up and your vehicle stuck if you try to run the code now, we should restructure the logic. So we need to find the part where the car is moved which is here:
// move car
if (arrowUp)
{
moveCar(timeDiff);
centerMap();
checkCollisions();
}
Now we need to restructure this to make it so the car will move no matter what the input, and that hitting the up arrow will accelerate the car:
// move car
if (arrowUp)
{
speed += acceleration * timeDiff;
if(speed > maxSpeed)
{
speed = maxSpeed;
}
}
moveCar(timeDiff);
centerMap();
checkCollisions();
Now we add some acceleration decay for when the accelerator is not being held down:
// move car
if (arrowUp)
{
speed += acceleration * timeDiff;
if(speed > maxSpeed)
{
speed = maxSpeed;
}
}
else //decay acceleration
{
speed -= friction * timeDiff;
if(speed < 0)
speed = 0;
}
moveCar(timeDiff);
centerMap();
checkCollisions();
to finish this off, we're going to make a bit easier to stop since now the car doesn't immediately stop when you let go of the accelerator, so I'm going to make a brake, which we're going to bind to the space bar. So first we need to declare our braking power:
static const braking_power:Number = acceleration * 2;
then we need our key for the brake:
// game variables
private var arrowLeft, arrowRight, arrowUp, arrowDown,spaceBar:Boolean; //spaceBar added to this
then we need to modify the key up and key down functions to add in the new key:
// note key presses, set properties
public function keyDownFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
arrowLeft = true;
} else if (event.keyCode == 39) {
arrowRight = true;
} else if (event.keyCode == 38) {
arrowUp = true;
} else if (event.keyCode == 40) {
arrowDown = true;
} else if (event.keyCode == Keyboard.SPACE) { //Space bar logic added here
spaceBar = true;
}
}
public function keyUpFunction(event:KeyboardEvent) {
if (event.keyCode == 37) {
arrowLeft = false;
} else if (event.keyCode == 39) {
arrowRight = false;
} else if (event.keyCode == 38) {
arrowUp = false;
} else if (event.keyCode == 40) {
arrowDown = false;
} else if (event.keyCode == Keyboard.SPACE) { //Space bar logic added here
spaceBar = false;
}
}
and lastly modify our logic to accomidate for braking:
// move car
if (arrowUp)
{
speed += acceleration * timeDiff;
if(speed > maxSpeed)
{
speed = maxSpeed;
}
}
else if(spaceBar)
{
speed -= braking_power * timeDiff;
if(speed < 0)
speed = 0;
}
else
{
speed -= friction * timeDiff;
if(speed < 0)
speed = 0;
}
moveCar(timeDiff);
centerMap();
checkCollisions();
and here's the end result:
(I know that acceleration is covered in the next chapter but I don't like the way he implements acceleration decay and doesn't have a brake)