2

我的滚动条正在工作,但我无法移动 player

播放器居中于窗口屏幕的中间。地图以玩家的位置为中心。

窗口(程序)大小为 640 x 640 像素

地图map.tmx正交的(塞尔达/口袋妖怪风格的游戏)。

仅使用 UP、DOWN、RIGHT、LEFT 键(无对角线移动)。

我在计算或做坏事?

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;
        }

    }

}
4

1 回答 1

1

基本思路:

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

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

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

在我的例子中工作

于 2012-11-21T19:03:46.960 回答