Game Development Reference
In-Depth Information
Our drawn 45-degree angles in Figure 10-10 are not exact, but they demonstrate basically where
the look-ahead points would be on a 0-degree angle for our player object.
The checkInput function
The checkInput function is called on each frame tick by the STATE_SYSTEM_GAMEPLAY state. It
evaluates key presses stored in the keyPressList array.
private function checkInput():void {
if (keyPressList[KEY_UP]){
player.velocity+=player.acceleration;
if (player.velocity >player.maxVelocity) player.velocity=player.maxVelocity;}
if (!keyPressList[KEY_UP] && player.velocity >0) {
player.velocity-=player.deceleration;
if (player.velocity <0) player.velocity=0;
}
if (keyPressList[KEY_DOWN]){
player.velocity-=player.acceleration;
if (player.velocity < -player.maxVelocity*player.reverseVelocityModifier)
player.velocity = -player.maxVelocity*player.reverseVelocityModifier;
}
if (!keyPressList[KEY_DOWN] && player.velocity <0) {
player.velocity+=player.deceleration;
if (player.velocity > 0) player.velocity = 0;
}
if (keyPressList[KEY_LEFT]){
player.nextRotation-=(player.velocity)*player.turnSpeed;
}
if (keyPressList[KEY_RIGHT]){
player.nextRotation+=(player.velocity)*player.turnSpeed;
}
}
Here is a detailed description of the main points of this function:
If the up key is pressed, we add acceleration ( player.acceleration ) to the velocity
until the velocity is at the maximum value. The velocity variable we use simple holds
the current speed of the car. It is not a true vector.
If the up key in not pressed and the velocity greater than zero, we subtract the
deceleration value from the velocity .
If the down key is pressed, we subtract the acceleration from the velocity until the
velocity is at the minimum value ( velocity * reverseVelocityModifier ).
If the velocity is less than 0 and the down key is not pressed, we add the deceleration
to the velocity until it reaches 0.
Search WWH ::




Custom Search