Question

My scroller is working but I can't move player.

Player is centering to middle of window screen. And map is centering to player's position.

Window (program) size is 640 x 640 px

Map map.tmx is orthogonal (zelda/pokemon style game).

Using only UP, DOWN, RIGHT, LEFT keys (no diagonal moves).

what I am calculating or doing bad ?

public class Play extends BasicGameState {

    Animation movingUp, movingDown, movingLeft, movingRight;
    Animation player;
    String playerName = "Test";
    boolean quit = false;
    int[] duration = {200, 200, 200};
    int playerX = 0;
    int playerY = 0;
    int cameraX;
    int cameraY;
    int screenWidth;
    int screenHeight;
    private TiledMap map = null;
    private static final float SPEED = 0.1f;

    public Play(int state, float x, float y) {
        playerX = (int) x;
        playerY = (int) y;
    }

    @Override
    public void init(GameContainer gc, StateBasedGame sbg) throws SlickException {

        map = new TiledMap("map/map.tmx");

        Image[] walkUp = {
            new Image("graphics/player/up0.png"),
            new Image("graphics/player/up1.png"),
            new Image("graphics/player/up2.png")
        };

        Image[] walkDown = {
            new Image("graphics/player/down0.png"),
            new Image("graphics/player/down1.png"),
            new Image("graphics/player/down2.png")
        };
        Image[] walkLeft = {
            new Image("graphics/player/left0.png"),
            new Image("graphics/player/left1.png"),
            new Image("graphics/player/left2.png")
        };
        Image[] walkRight = {
            new Image("graphics/player/right0.png"),
            new Image("graphics/player/right1.png"),
            new Image("graphics/player/right2.png")
        };

        movingUp = new Animation(walkUp, duration, false);
        movingDown = new Animation(walkDown, duration, false);
        movingLeft = new Animation(walkLeft, duration, false);
        movingRight = new Animation(walkRight, duration, false);

        player = movingDown;

    }

    @Override
    public void render(GameContainer gc, StateBasedGame sbg, Graphics g) throws SlickException {

        screenWidth = gc.getWidth();
        screenHeight = gc.getHeight();

        cameraX = (screenWidth / 2) - (playerX / 2);
        cameraY = (screenHeight / 2) - (playerY / 2);

        map.render(playerX, playerY);

        player.draw(cameraX, cameraY);

        g.drawString("X: " + playerX + "\nY: " + playerY, 520, 20);
        g.resetTransform();

    }

    @Override
    public void update(GameContainer gc, StateBasedGame sbg, int delta) throws SlickException {
        Input input = gc.getInput();

        if (input.isKeyDown(Input.KEY_UP)) {
            player = movingUp;
            player.update(delta);
            playerY += delta * SPEED;
        } else if (input.isKeyDown(Input.KEY_DOWN)) {
            player = movingDown;
            player.update(delta);
            playerY -= delta * SPEED;
        } else if (input.isKeyDown(Input.KEY_LEFT)) {
            player = movingLeft;
            player.update(delta);
            playerX += delta * SPEED;
        } else if (input.isKeyDown(Input.KEY_RIGHT)) {
            player = movingRight;
            player.update(delta);
            playerX -= delta * SPEED;
        }

    }

}
Était-ce utile?

La solution

Basic idea:

screenWidth = gc.getWidth();
screenHeight = gc.getHeight();

cameraX = (screenWidth / 2) - (playerX / 2);
cameraY = (screenHeight / 2) - (playerY / 2);

map.render(playerX, playerY);
player.draw(cameraX, cameraY);

Working in my example

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top