Game Development Reference
In-Depth Information
These are the major points you need to know about the checkInput function:
The game does not accept player key input if the player tank is currently moving through
a tunnel. This greatly simplifies the code that handles movement when the player is off
the screen (in a tunnel). The inTunnel attribute of the TileByTileBlitSprite class is
used in this evaluation. When the player leaves the screen and goes into a tunnel, this
will be set to true .
The checkTile function is used to check if the next tile the player is trying to move to is a
valid TILE_MOVE tile (see Chapter 6 for the difference between TILE_WALL and
TILE_MOVE ).
If the checkTile function returns true for the desired move direction, more evaluations
are made. These evaluations mostly hinge on whether the player is trying to turn.
Turning is the most complicated procedure in this type of movement structure, because
the player should only be able to turn into an open space.
If the direction to move is the same as the current direction the player is moving in, or
the exact opposite direction, or if the player is stopped, the move is valid and the
switchMovement function is called.
If the direction to move is not the same as the current direction, is not the opposite of the
current direction, or the player is not stopped, we must evaluate further. This means the
player is trying to turn. The player will only be allowed to make the turn from the center
of the current tile. This situation is handled with a call to the checkCenterTile function.
I coded the movement in this game to mimic a few of the attributes of Pac-Mac that I wanted to
try out. In Pac-Man, the player cannot turn in a direction unless the next tile in that direction is a
TILE_WALK tile. Some might think that this means the player tank cannot rotate to free itself when
stuck in a corner. This assumption would be a false. Just like Pac-Man, the tank cannot get stuck
in any corners because the player can always just press arrow for the opposite direction of
movement with out having to rotate to that position.
Adding the checkTile function
The checkTile function has a single purpose: to evaluate whether or not a TileByTileBlitSprite
instance can legally move into a tile. It does this by first evaluating what the next tile will be and
then checking the tile number in the tileSheetData array to see if it is a TILE_MOVE or TILE_WALL
tile. The function accepts a direction represented by the integer value, direction , and an
instance of the TileByTileByTileSprite class represented by the object variable.
private function checkTile(direction:int, object:TileByTileBlitSprite):Boolean {
var row:int = object.currRow;
var col:int = object.currCol;
switch(direction) {
case MOVE_UP:
row--;
if (row <0) {
row = mapRowCount-1;
}
Search WWH ::




Custom Search