Game Development Reference
In-Depth Information
Adding the checkCenterTile function
The
checkCenterTile
function simply takes the passed in
TileByTileBlitSprite
instance and
evaluates whether or not it is in the center of the current tile. If it is, it sends back
true
; if not, it
sends back
false
.
private function checkCenterTile( object:TileByTileBlitSprite):Boolean {
var xCenter:int = (object.currCol * tileWidth) + (.5 * tileWidth);
var yCenter:int= (object.currRow * tileHeight) + (.5 * tileHeight);
if (object.x == xCenter && object.y == yCenter) {
//trace("in center tile");
return true;
}else {
//trace("not in center tile");
return false;
}
}
The
x
and
y
values for a tile represent the top left-hand corner of the tile. To find the current
x
and
y
values, we multiply the
currCol
by the
tileWidth
and the
currRow
by the
tileHeight
. Then, we
add half the tile width to the
x
value and half the
tileheight
to the
y
value. This calculates the
xCenter
and
yCenter
values, which are then checked against the
x
and
y
values for our object.
The game objects are offset from the center of the tiles because they are centered around the
midpoint of the
Sprite
holder. If they were not, we would have to add half the
tileWidth
and
tileHeight
to the
x
and
y
values of the object also.
Adding the switchMovement function
The
switchMovement
function takes in the
direction
variable as a parameter to represent the
direction that object is going to move in. It also takes in a
TileByTileBlitSprite
reference as the
object
parameter variable. Based on the passed-in direction, the function simply sets properties
of the
object
to match the new direction. Here is the code for the functions:
private function switchMovement(direction:int, object:TileByTileBlitSprite):void{
switch(direction) {
case MOVE_UP:
//trace("move up");
object.rotation = 0;
object.dx = 0;
object.dy = -1;
object.currentDirection = MOVE_UP;
object.animationLoop = true;
break;
case MOVE_DOWN:
//trace("move down");
object.rotation = 180;
object.dx = 0;
object.dy = 1;
object.currentDirection = MOVE_DOWN;
object.animationLoop = true;
break;