Game Development Reference
In-Depth Information
public function startGame():void {
addEventListener(KeyboardEvent.KEY_DOWN, keyDown,
false, 0, true);
addEventListener(KeyboardEvent.KEY_UP, keyUp,
false, 0, true);
addEventListener(Event.ENTER_FRAME, gameLoop,
false, 0, true);
}
protected function gameLoop(e:Event):void {
if (stage.focus != this) stage.focus = this;
readInput();
moveVehicle();
}
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.max
Steering * (vehicle.speed / Vehicle.maxSpeed));
if (_leftPressed) vehicle.angle -= (Vehicle.max
Steering * (vehicle.speed / Vehicle.maxSpeed));
if (_spacePressed) {
vehicle.speed *= Vehicle.handBrakeFriction;
vehicle.acceleration = 0;
}
}
protected function moveVehicle():void {
if (!vehicle.acceleration) vehicle.speed *=
_friction;
vehicle.speed += vehicle.acceleration;
vehicle.x += Math.cos(vehicle.angle) * (vehicle.
speed * Time.deltaTime);
vehicle.y += Math.sin(vehicle.angle) * (vehicle.
speed * Time.deltaTime);
}
protected function keyDown(e:KeyboardEvent):void {
if (e.keyCode == Keyboard.LEFT) _leftPressed = true;
if (e.keyCode == Keyboard.RIGHT) _rightPressed = true;
if (e.keyCode == Keyboard.UP) _upPressed = true;
if (e.keyCode == Keyboard.DOWN) _downPressed = true;
Search Nedrilad ::




Custom Search