Game Development Reference
In-Depth Information
code needs to be added. The ellipses represent all of the other code in the function, so we don't
need to waste space repeating it; we will continue to use this formatting throughout this chapter.
private function restartPlayer(afterDeath:Boolean=false):void {
player.visible = true;
player.currCol = playerStartCol;
playerInvincibleCountDown = true;
playerInvincibleCount = 0;
. . .
/this is as good a place as any for now
addEventListener(Event.ENTER_FRAME, runGame, false,0,true);
}
Next, we will add in the new very basic game loop function. Create the following new function:
public function runGame(e:Event):void {
trace("run game");
}
If you test this code, you should see this output:
run game
run game
run game
run game
The output will continue repeatedly. So, we have the
runGame
function being called repeatedly,
but we don't have it doing anything yet. Let's bite off moving the player about the maze in
simple chunks.
Switching player move state with the arrow keys
Before we get to the actual movement of the player, we need to set up all of the necessary
functions to control changing the state. The move state of the player is contained in the
BlitSprite
class instance attribute,
currentDirection
. The
currentDirection
variable can be
set to be one of five states, which are the
GameDemoInteration2.as
constants:
MOVE_UP
,
MOVE_DOWN
,
MOVE_LEFT
,
MOVE_RIGHT
, and
MOVE_STOP
. The
currentDirection
variable doesn't
actually represent the
rotation
of the tank (that is held in the
Sprite.rotation
property). For this
reason, we can use
STOP
as a direction. A
TileByTileBlitSprite
can have a
currentDirection
of
MOVE_STOP
and a rotation of
0
(facing up). Also (for instance), a
TileByTileBlitSprite
can
have a rotation of
90
(turned to the right) and a
currentDirection
of
MOVE_RIGHT
. But, a
TileByTileBlitSprite
cannot have a rotation of
-90
(left) and a
currentDirection
of anything
but
MOVE_LEFT
(for instance). These two properties,
currentDirection
and
rotation
, are used in
conjunction to facilitate moving about the maze.
In a nutshell, unless the
currentDirection
is
MOVE_STOP
the
currentDirection
and rotation
must match.
We are now going to add some functions and code to the game file in order to be able to check if
the desired movement is valid and used to actually change the rotation of the player. Once you