Game Development Reference
In-Depth Information
player.inTunnel = false;
}
break;
case MOVE_RIGHT:
if (player.nextX == xTunnelMax) {
player.nextX = xTunnelMin;
}else if (player.nextX == xMin) {
player.inTunnel = false;
}
break;
case MOVE_STOP:
trace("stopped");
break;
}
}
player.currRow = player.nextY / tileWidth;
player.currCol = player.nextX / tileHeight;
player.updateCurrentTile();
//*** end added iteration #4
}
These are the major points you need to know about the update function:
The
nextX
and
nextY
properties of the player object are updated by adding the
dx
and
dy
values multiplied by the
moveSpeed
of the object. The
player.moveSpeed
value is
2
.
It should be noted that the tile width and height must be evenly divisible by the
moveSpeed
of any object that when we are trying to detect the center of a tile. The
moveSpeed
value is set to
2
by default in the
BlitSprite
class, but it can be set to 1, 2,
4, 8, 16, and 32. 1, 2, 4, and 8 will have the best results and the most playable control
for the character.
Do not
use an odd number other than 1, or the game objects may
never detect the center of the tile.
If you have played a poor implementation of a Pac-Man style game or even an emulated
version with poor input controls, you will find that it is very easy to overshoot a tunnel
when you actually want to turn. The control scheme we have implemented here goes to
great lengths to ensure that this lack of control does not occur. We actually stop the
player's forward movement when the character reaches the center of a tile unless a key
is held down to move the character in a valid direction. If the player presses an arrow to
move in a direction to turn, the turn will occur right away when the center is reached.
If we were making a Pac-Man style game, where the player does not stop at the center of each
tile, we would just need to eliminate
the swtitchMovement(MOVE_STOP)
function call in the
render
function.
The bulk of the update code occurs if the player is in a tunnel, because once the
player has moved off the screen, we need to take control of the character and force it
to the other side. At the end of this iteration, you will notice that once you enter a