Game Development Reference
In-Depth Information
I divide the difference by 1000 when I return it. This could easily be
modified to return milliseconds instead, if that
'
s what you prefer. It
'
s
mainly important to pick a convention and stick with it. We
'
ll now
look at how method class is used in the Game class.
The Game Class
Now we
'
ve come to the core of the functionality and the math that
we
ll need to employ. It also functions as the document class for
the accompanying FLA. The class starts out with just a few
declarations.
'
protected var _leftPressed:Boolean;
protected var _rightPressed:Boolean;
protected var _upPressed:Boolean;
protected var _downPressed:Boolean;
protected var _spacePressed:Boolean;
protected var _friction:Number = .95;
ll use, so we can
know whether that key is being pressed. There is also a value for
friction, or rather the coefficient of friction of the surface the vehi-
cle will be driving on. This value will cause the vehicle to slow
down when it is not accelerating.
There are Boolean values for each key we
'
public function Game() {
addEventListener(Event.ADDED_TO_STAGE, addedToStage,
false, 0, true);
}
private function addedToStage(e:Event):void {
startGame();
}
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);
}
When the game is added to the Stage, it triggers startGame. This
method sets up listeners for both keyboard input and the enter-
Frame cycle. We
'
ll look at the keyDown and keyUp methods next.
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;
Search Nedrilad ::




Custom Search