Game Development Reference
In-Depth Information
case MOVE_LEFT:
//trace("move left");
object.currentDirection = MOVE_LEFT;
object.rotation = -90;
object.dx = -1;
object.dy = 0;
object.animationLoop = true;
break;
case MOVE_RIGHT:
//trace("move right");
object.currentDirection = MOVE_RIGHT;
object.rotation = 90;
object.dx = 1;
object.dy = 0;
object.animationLoop = true;
break;
case MOVE_STOP:
//trace("move stop");
object.currentDirection = MOVE_STOP;
object.dx = 0;
object.dy = 0;
object.animationLoop = false;
break;
}
object.nextRow = object.currRow + object.dy;
object.nextCol = object.currCol + object.dx;
}
These are the major points you need to know about the
switchMovement f
unction:
Based on the passed in
direction
value, it sets the
currentDirection
of the passed in
TileByTileBlitSprite
object property to the matching constant move state variable.
Based on the
direction
variable, it sets the
rotation
of the
object
to match.
Based on the
direction
variable, the delta x (
dx
) and delta y (
dy
) attributes of the object
are changed. These are used in the
update
function to move the object along the vertical
or horizontal axis of the game play field. Because we only move up, down, right, or left,
one will always be 1 (or -1), and the other will always be 0 unless the object is stopped.
In this case, they will both be 0.
If the object is moving in a direction (not stopped), its
object.animationLoop
property is
set to
true
.
If the object is stopped, the
object.animationLoop
property is set to
false
.
Finally, the
nextRow
and
nextCol
attributes of the object are set.
Testing iteration 3
When you run this iteration, you will see that the tank can be rotated to any open position but not
to one where a
TILE_WALL
tile is the next tile. In Figure 7-2, you will see the tank pointing down. It
cannot point up, but it can point left and right. Don't worry if this looks limiting. When we add the
tank movement in the next iteration, you will find the movement to be very well suited to this style
of game.