Skip to main content
I added comments to my code and edited a line or two for the reader's simplicity.
Source Link
helsont
  • 285
  • 2
  • 10
// 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();
}
private final int STILL = 0, LEFT = 1, UP = 2, RIGHT = 3, DOWN = 4,
        STOP = 5;

private void move() {
    if (movement == UP) {
        moveUp();
    }
    if (movement == RIGHT) {
        moveRight();
    }
    ....
    if (movement == STILL) {
        setDx(0);
        setDy(0);
    }
}
private void moveLeft() {
    if (!tileIsOccupied(LEFT)) {
        setTimeMoved(getCurrentTime());
        Tile tile = getGrid().getTiles()[getTileRow() - 1][getTileColumn()];
        if (getXpos() > tile.getXpos()) {
            setDx(-1);
            setXpos(getXpos() + getDx());
        }
        if (getXpos() <= tile.getXpos()) {
            setTileColumn(tile.getTileColumn());
            setTileRow(tile.getTileRow());
            setXpos(tile.getXpos());
            setMovement(STILL);
        }
    } else
        movement = STILL;
}

public void update(int vpX, int vpY) {
    setViewX(vpX);
    setViewY(vpY);
    move();
}
// 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 == LEFT) {
        moveLeft();
    }
    ....
    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;
}

// It's essential that the update function be called on a loop. Otherwise, the
// player will not move. 
public void update() {
    move();
}
Improved (pretty much) final product
Source Link
helsont
  • 285
  • 2
  • 10

This is the code I used. It only works for one keystroke, (and currently it's not very elegant) but it will move the player up one tile in the way I want it to. Hope it helps someone.

publicprivate voidfinal update(int vpXSTILL = 0, intLEFT vpY)= {
1, UP = 2, setViewX(vpX);
RIGHT = 3, DOWN setViewY(vpY);= 4,
    //move();
    
 STOP = 5;

private void if(isMovingUpmove()) {
     if (movement == setTimeMoved(getCurrentTime()UP); {
        if(!tileIsOccupiedmoveUp(2));
 {   }
    if (movement == RIGHT) {
    Tile tile = getGrid().getTiles()[getTileRow()][getTileColumn moveRight()-1];;
    }
    ....
    if(getYpos()>tile.getYpos ()movement == STILL) {
        setDx(0);
        setDy(-10);
    }
}
private void moveLeft() {
    if (!tileIsOccupied(LEFT)) {
   setYpos(getYpos() + getDy   setTimeMoved(getCurrentTime());
           Tile tile = getGrid().getTiles()[getTileRow() - System.out.println1][getTileColumn("adjusted");];
        if (getXpos() > tile.getXpos()) }{
            else {setDx(-1);
                System.out.printlnsetXpos("MygetXpos() Y:"+getYpos+ getDx());
        }
        System.out.printlnif ("HisgetXpos() Y:"+tile<= tile.getYposgetXpos()); {
            }setTileColumn(tile.getTileColumn());
            if(getYpossetTileRow()<=tiletile.getYposgetTileRow()) {;
                setYpossetXpos(tile.getYposgetXpos());
                setMovingsetMovement(falseSTILL);
        }
    } else
        movement = STILL;
} 

public void update(int vpX, int vpY) }{
    //checkCollision(getGridsetViewX()vpX);
    updateRectanglessetViewY(vpY);
    repaintmove();
}

This is the code I used. It only works for one keystroke, (and currently it's not very elegant) but it will move the player up one tile in the way I want it to. Hope it helps someone.

public void update(int vpX, int vpY) {
    setViewX(vpX);
    setViewY(vpY);
    //move();
    
     if(isMovingUp()) {
        setTimeMoved(getCurrentTime());
        if(!tileIsOccupied(2)) {
            Tile tile = getGrid().getTiles()[getTileRow()][getTileColumn()-1];
            if(getYpos()>tile.getYpos()) {
                setDy(-1);
                setYpos(getYpos() + getDy());
                System.out.println("adjusted");
            }
            else {
                System.out.println("My Y:"+getYpos());
                System.out.println("His Y:"+tile.getYpos());
            }
            if(getYpos()<=tile.getYpos()) {
                setYpos(tile.getYpos());
                setMoving(false);
            }
        }
    }
    //checkCollision(getGrid());
    updateRectangles();
    repaint();
}

This is the code I used.

private final int STILL = 0, LEFT = 1, UP = 2, RIGHT = 3, DOWN = 4,
        STOP = 5;

private void move() {
    if (movement == UP) {
        moveUp();
    }
    if (movement == RIGHT) {
        moveRight();
    }
    ....
    if (movement == STILL) {
        setDx(0);
        setDy(0);
    }
}
private void moveLeft() {
    if (!tileIsOccupied(LEFT)) {
        setTimeMoved(getCurrentTime());
        Tile tile = getGrid().getTiles()[getTileRow() - 1][getTileColumn()];
        if (getXpos() > tile.getXpos()) {
            setDx(-1);
            setXpos(getXpos() + getDx());
        }
        if (getXpos() <= tile.getXpos()) {
            setTileColumn(tile.getTileColumn());
            setTileRow(tile.getTileRow());
            setXpos(tile.getXpos());
            setMovement(STILL);
        }
    } else
        movement = STILL;
} 

public void update(int vpX, int vpY) {
    setViewX(vpX);
    setViewY(vpY);
    move();
}
Source Link
helsont
  • 285
  • 2
  • 10

This is the code I used. It only works for one keystroke, (and currently it's not very elegant) but it will move the player up one tile in the way I want it to. Hope it helps someone.

public void update(int vpX, int vpY) {
    setViewX(vpX);
    setViewY(vpY);
    //move();
    
    if(isMovingUp()) {
        setTimeMoved(getCurrentTime());
        if(!tileIsOccupied(2)) {
            Tile tile = getGrid().getTiles()[getTileRow()][getTileColumn()-1];
            if(getYpos()>tile.getYpos()) {
                setDy(-1);
                setYpos(getYpos() + getDy());
                System.out.println("adjusted");
            }
            else {
                System.out.println("My Y:"+getYpos());
                System.out.println("His Y:"+tile.getYpos());
            }
            if(getYpos()<=tile.getYpos()) {
                setYpos(tile.getYpos());
                setMoving(false);
            }
        }
    }
    //checkCollision(getGrid());
    updateRectangles();
    repaint();
}