2

スクローラーは動作していますが、player を移動できません

プレイヤーはウィンドウ画面の中央に位置しています。そしてマップはプレイヤーの位置を中心にしています。

ウィンドウ(プログラム)のサイズは 640 x 640 px

マップ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 に答える