我的滚动条正在运行,但我无法移动播放器。
玩家居中于窗口屏幕的中间位置。地图以玩家的位置为中心。
窗口(程序)大小为640 x 640 px
地图 map.tmx 是正交(塞尔达/口袋妖怪风格游戏)。
仅使用向上,向下,向右,向左键(无对角移动)。
我在计算或做坏事?
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;
}
}
}
答案 0 :(得分: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);
在我的例子中工作