Game Development Reference
In-Depth Information
if (e.keyCode == Keyboard.DOWN) _downPressed = true;
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;
}
These two functions simply toggle the different Boolean values
to either true or false as keyboard input is received.
protected function gameLoop(e:Event):void {
if (stage.focus != this) stage.focus = this;
readInput();
moveVehicle();
}
re dealing with keyboard input, which automatically
focuses on the Stage, on each frame cycle we make sure that the
game still has focus, even if the player were to click somewhere
else on the screen. It then calls readInput and moveVehicle ,bothof
which we
Because we
'
'
ll look at next.
protected function readInput():void {
if (_upPressed) vehicle.acceleration += Vehicle.
accelerationRate * Time.deltaTime;
if (_downPressed) vehicle.acceleration -= Vehicle.
accelerationRate * Time.deltaTime;
if (!_upPressed && !_downPressed) vehicle.acceleration = 0;
if (_rightPressed) vehicle.angle += (Vehicle.maxSteering *
(vehicle.speed / Vehicle.maxSpeed));
if (_leftPressed) vehicle.angle -= (Vehicle.maxSteering *
(vehicle.speed / Vehicle.maxSpeed));
if (_spacePressed) {
vehicle.speed *= Vehicle.handBrakeFriction;
vehicle.acceleration = 0;
}
}
This method runs through all the key-related Boolean values. If
the up or down arrows are pressed, it applies acceleration. If the
right and left arrows are pressed, it applies steering based on the
speed of the vehicle. Finally, if the space bar is pressed, it applies
the hand brake friction to the vehicle
'
s speed and resets any
acceleration.
Search Nedrilad ::




Custom Search