// These numbers represent the different directions in movement.
private final int STILL = 0, LEFT = 1, UP = 2, RIGHT = 3, DOWN = 4,
STOP = 5;
// The current direction of movement of the player.
public int movement = 0;
// The move method, like any other player class.
private void move() {
// The keyboard input class works like this:
// If the up keyboard button is pressed, player.movement = player.UP,
// if right is pressed, player.movement = player.RIGHT, etc.
// and if no key is pressed, player.movement = player.STILL;
if (movement == UP) {
moveUp();
}
if (movement == RIGHTLEFT) {
moveRightmoveLeft();
}
....
if (movement == STILL) {
setDx(0);
setDy(0);
}
}
private void moveLeft() {
// Because this intends to work on a tile grid, this method checks if
// the tile to the left of the player is occupied.
if (!tileIsOccupied(LEFT)) {
// The player can press a key every 200 ms. Sets the time moved to this
// point in time. If a player attempts to move before those 200 ms are
// over, no movement will occur.
setTimeMoved(getCurrentTime());
// This tile is essential the tile to the left of the player.
Tile tile = getGrid().getTiles()[getTileRow() - 1][getTileColumn()];
// If the xpos of the player is greater than the xpos of the tile, keep moving
// left.
if (getXpos() > tile.getXpos()) {
setDx(-1);
setXpos(getXpos() + getDx());
}
// If the xpos of the player is less than or equal to that of the tile,
// set the player's position equal to the tiles. In other words, when
// the player has moved to the tile.
// Set this current tile to equal the player's tile, and movement is reset to
// still.
if (getXpos() <= tile.getXpos()) {
setTileColumn(tile.getTileColumn());
setTileRow(tile.getTileRow());
setXpos(tile.getXpos());
setMovement(STILL);
}
} else
movement = STILL;
}
public// voidIt's essential that the update(int vpX,function intbe vpY)called {
on a loop. Otherwise, setViewX(vpX);the
// player will not setViewYmove.
public void update(vpY); {
move();
}