在 Java 中对玩家进行 "Camera" 定位
Having The "Camera" positioning On The Player in Java
因此,我在将 "camera" 定位在播放器上时遇到了一些麻烦,因为这是我第一次尝试完成此操作。每当我启动我的游戏并移动我的播放器时,相机似乎并没有像我希望的那样以播放器为中心,我不知道它有什么问题。我尝试浏览在线资源并检查了有关堆栈溢出的许多问题,但我找不到我做错了什么:/。我正在使用 Java 并使用 Slick2D 引擎。这是我的 class 个文件:
Camera.class:
private float offsetMaxX;
private float offsetMaxY;
private float offsetMinX = 0;
private float offsetMinY = 0;
public float camX;
public float camY;
public Camera () {
super ();
}
public void update (float playerX, float playerY, float mapX, float mapY){
offsetMaxX = mapX - Main.WIDTH;
offsetMaxY = mapY - Main.HEIGHT;
camX = playerX - Main.WIDTH/2;
camY = playerY - Main.HEIGHT/2;
if (camX > offsetMaxX) {
camX = offsetMaxX;
} else if (camX < offsetMinX) {
camX = offsetMinX;
}
if (camY > offsetMaxY) {
camY = offsetMaxY;
} else if (camY < offsetMinY) {
camY = offsetMinY;
}
}
public float getCamX() {
return camX;
}
public float getCamY() {
return camY;
}
public float getOffsetMaxX() {
return offsetMaxX;
}
public float getOffsetMaxY() {
return offsetMaxY;
}
public float getOffsetMinX() {
return offsetMinX;
}
public float getOffsetMinY() {
return offsetMinY;
}
public void setOffsetMaxX(float offsetMaxX) {
this.offsetMaxX = offsetMaxX;
}
public void setOffsetMaxY(float offsetMaxY) {
this.offsetMaxY = offsetMaxY;
}
public void setOffsetMinX(float offsetMinX) {
this.offsetMinX = offsetMinX;
}
public void setOffsetMinY(float offsetMinY) {
this.offsetMinY = offsetMinY;
}
还有我的Game.class:
// Player variables
public SpriteSheet playerSS = null;
protected InputHandler inputHandler;
public float playerX = Main.WIDTH/2, playerY = Main.HEIGHT/2;
private Animation sprite, runningAnimationLEFT, runningAnimationRIGHT, runningAnimationUP, runningAnimationDOWN;
private Animation standLEFT, standRIGHT, standUP, standDOWN; //THIS IS TO PREVENT ANIMATIONS FROM CONTINUING AFTER THE PLAYER STOPS
private int duration = 300;
public boolean isMoving, isLeft, isRight, isUp, isDown;
//Map
private Image testMap; //This is a test map
private float mapX = 1024, mapY = 1024;
//Camera
public Camera camera;
public Game (int stateID) {}
@Override
public int getID() {
return Main.game; //GAMESTATE = 1
}
@Override
public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException {
playerSS = new SpriteSheet("res/characters/player_sprite_sheet.png", 50, 75);
testMap = new Image ("res/grassMap.png");
camera = new Camera();
// ANIMATIONS
runningAnimationDOWN = new Animation();
runningAnimationDOWN.setAutoUpdate(true);
runningAnimationDOWN.addFrame(playerSS.getSprite(1, 0), duration);
runningAnimationDOWN.addFrame(playerSS.getSprite(0, 0), duration);
runningAnimationDOWN.addFrame(playerSS.getSprite(2, 0), duration);
runningAnimationLEFT = new Animation();
runningAnimationLEFT.setAutoUpdate(true);
runningAnimationLEFT.addFrame(playerSS.getSprite(1, 1), duration);
runningAnimationLEFT.addFrame(playerSS.getSprite(0, 1), duration);
runningAnimationLEFT.addFrame(playerSS.getSprite(2, 1), duration);
runningAnimationRIGHT = new Animation();
runningAnimationRIGHT.setAutoUpdate(true);
runningAnimationRIGHT.addFrame(playerSS.getSprite(1, 2), duration);
runningAnimationRIGHT.addFrame(playerSS.getSprite(0, 2), duration);
runningAnimationRIGHT.addFrame(playerSS.getSprite(2, 2), duration);
runningAnimationUP = new Animation();
runningAnimationUP.setAutoUpdate(true);
runningAnimationUP.addFrame(playerSS.getSprite(1, 3), duration);
runningAnimationUP.addFrame(playerSS.getSprite(0, 3), duration);
runningAnimationUP.addFrame(playerSS.getSprite(2, 3), duration);
standDOWN = new Animation();
standDOWN.setAutoUpdate(true);
standDOWN.addFrame(playerSS.getSprite(1, 0), duration);
standLEFT = new Animation();
standLEFT.setAutoUpdate(true);
standLEFT.addFrame(playerSS.getSprite(1, 1), duration);
standRIGHT = new Animation();
standRIGHT.setAutoUpdate(true);
standRIGHT.addFrame(playerSS.getSprite(1, 2), duration);
standUP = new Animation();
standUP.setAutoUpdate(true);
standUP.addFrame(playerSS.getSprite(1, 3), duration);
sprite = standDOWN;
}
@Override
public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta) throws SlickException {
inputHandler.updateKeyboardRelay(gameContainer);
isMoving = false;
if (inputHandler.keyUP) {
isUp = true;
isDown = false;
isLeft = false;
isRight = false;
isMoving = true;
playerY -= delta * 0.1f;
} else if (inputHandler.keyDOWN) {
isDown = true;
isUp = false;
isLeft = false;
isRight = false;
isMoving = true;
playerY += delta * 0.1f;
} else if (inputHandler.keyLEFT) {
isLeft = true;
isUp = false;
isDown = false;
isRight = false;
isMoving = true;
playerX -= delta * 0.1f;
} else if (inputHandler.keyRIGHT) {
isRight = true;
isUp = false;
isDown = false;
isLeft = false;
isMoving = true;
playerX += delta * 0.1f;
}
if (isMoving) {
if (inputHandler.keyUP) {
sprite = runningAnimationUP;
sprite.update(delta);
} else if (inputHandler.keyDOWN) {
sprite = runningAnimationDOWN;
sprite.update(delta);
} else if (inputHandler.keyLEFT) {
sprite = runningAnimationLEFT;
sprite.update(delta);
} else if (inputHandler.keyRIGHT) {
sprite = runningAnimationRIGHT;
sprite.update(delta);
}
} else {
if (isDown) {
sprite = standDOWN;
sprite.update(delta);
} else if (isUp) {
sprite = standUP;
sprite.update(delta);
} else if (isLeft) {
sprite = standLEFT;
sprite.update(delta);
} else if (isRight) {
sprite = standRIGHT;
sprite.update(delta);
}
}
// Map Boundaries
if (playerX > mapX) {
playerX = mapX;
} else if (playerX < 0) {
playerX = 0;
}
if (playerY > mapY) {
playerY = mapY;
} else if (playerY < 0) {
playerY = 0;
}
camera.update(playerX, playerY, mapX, mapY);
}
@Override
public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
testMap.draw(mapX - 1024, mapY - 1024);
graphics.translate(camera.getCamX(), camera.getCamY());
sprite.draw(playerX, playerY);
graphics.translate(-camera.getCamX(), -camera.getCamY());
graphics.drawString("Player X: " + playerX, 400, 400);
graphics.drawString("Player Y: " + playerY, 400, 415);
}
我觉得我的 Camera.class 和我的 update(...) & render(...) 方法有问题,但我不知道是什么。
这里在你的相机class更新方法中尝试改变
camX = playerX - Main.WIDTH/2;
camY = playerY - Main.HEIGHT/2;
至
camX = playerX;
camY = playerY;
如果这不起作用,请告诉我。
因此,我在将 "camera" 定位在播放器上时遇到了一些麻烦,因为这是我第一次尝试完成此操作。每当我启动我的游戏并移动我的播放器时,相机似乎并没有像我希望的那样以播放器为中心,我不知道它有什么问题。我尝试浏览在线资源并检查了有关堆栈溢出的许多问题,但我找不到我做错了什么:/。我正在使用 Java 并使用 Slick2D 引擎。这是我的 class 个文件:
Camera.class:
private float offsetMaxX;
private float offsetMaxY;
private float offsetMinX = 0;
private float offsetMinY = 0;
public float camX;
public float camY;
public Camera () {
super ();
}
public void update (float playerX, float playerY, float mapX, float mapY){
offsetMaxX = mapX - Main.WIDTH;
offsetMaxY = mapY - Main.HEIGHT;
camX = playerX - Main.WIDTH/2;
camY = playerY - Main.HEIGHT/2;
if (camX > offsetMaxX) {
camX = offsetMaxX;
} else if (camX < offsetMinX) {
camX = offsetMinX;
}
if (camY > offsetMaxY) {
camY = offsetMaxY;
} else if (camY < offsetMinY) {
camY = offsetMinY;
}
}
public float getCamX() {
return camX;
}
public float getCamY() {
return camY;
}
public float getOffsetMaxX() {
return offsetMaxX;
}
public float getOffsetMaxY() {
return offsetMaxY;
}
public float getOffsetMinX() {
return offsetMinX;
}
public float getOffsetMinY() {
return offsetMinY;
}
public void setOffsetMaxX(float offsetMaxX) {
this.offsetMaxX = offsetMaxX;
}
public void setOffsetMaxY(float offsetMaxY) {
this.offsetMaxY = offsetMaxY;
}
public void setOffsetMinX(float offsetMinX) {
this.offsetMinX = offsetMinX;
}
public void setOffsetMinY(float offsetMinY) {
this.offsetMinY = offsetMinY;
}
还有我的Game.class:
// Player variables
public SpriteSheet playerSS = null;
protected InputHandler inputHandler;
public float playerX = Main.WIDTH/2, playerY = Main.HEIGHT/2;
private Animation sprite, runningAnimationLEFT, runningAnimationRIGHT, runningAnimationUP, runningAnimationDOWN;
private Animation standLEFT, standRIGHT, standUP, standDOWN; //THIS IS TO PREVENT ANIMATIONS FROM CONTINUING AFTER THE PLAYER STOPS
private int duration = 300;
public boolean isMoving, isLeft, isRight, isUp, isDown;
//Map
private Image testMap; //This is a test map
private float mapX = 1024, mapY = 1024;
//Camera
public Camera camera;
public Game (int stateID) {}
@Override
public int getID() {
return Main.game; //GAMESTATE = 1
}
@Override
public void init(GameContainer gameContainer, StateBasedGame stateBasedGame) throws SlickException {
playerSS = new SpriteSheet("res/characters/player_sprite_sheet.png", 50, 75);
testMap = new Image ("res/grassMap.png");
camera = new Camera();
// ANIMATIONS
runningAnimationDOWN = new Animation();
runningAnimationDOWN.setAutoUpdate(true);
runningAnimationDOWN.addFrame(playerSS.getSprite(1, 0), duration);
runningAnimationDOWN.addFrame(playerSS.getSprite(0, 0), duration);
runningAnimationDOWN.addFrame(playerSS.getSprite(2, 0), duration);
runningAnimationLEFT = new Animation();
runningAnimationLEFT.setAutoUpdate(true);
runningAnimationLEFT.addFrame(playerSS.getSprite(1, 1), duration);
runningAnimationLEFT.addFrame(playerSS.getSprite(0, 1), duration);
runningAnimationLEFT.addFrame(playerSS.getSprite(2, 1), duration);
runningAnimationRIGHT = new Animation();
runningAnimationRIGHT.setAutoUpdate(true);
runningAnimationRIGHT.addFrame(playerSS.getSprite(1, 2), duration);
runningAnimationRIGHT.addFrame(playerSS.getSprite(0, 2), duration);
runningAnimationRIGHT.addFrame(playerSS.getSprite(2, 2), duration);
runningAnimationUP = new Animation();
runningAnimationUP.setAutoUpdate(true);
runningAnimationUP.addFrame(playerSS.getSprite(1, 3), duration);
runningAnimationUP.addFrame(playerSS.getSprite(0, 3), duration);
runningAnimationUP.addFrame(playerSS.getSprite(2, 3), duration);
standDOWN = new Animation();
standDOWN.setAutoUpdate(true);
standDOWN.addFrame(playerSS.getSprite(1, 0), duration);
standLEFT = new Animation();
standLEFT.setAutoUpdate(true);
standLEFT.addFrame(playerSS.getSprite(1, 1), duration);
standRIGHT = new Animation();
standRIGHT.setAutoUpdate(true);
standRIGHT.addFrame(playerSS.getSprite(1, 2), duration);
standUP = new Animation();
standUP.setAutoUpdate(true);
standUP.addFrame(playerSS.getSprite(1, 3), duration);
sprite = standDOWN;
}
@Override
public void update(GameContainer gameContainer, StateBasedGame stateBasedGame, int delta) throws SlickException {
inputHandler.updateKeyboardRelay(gameContainer);
isMoving = false;
if (inputHandler.keyUP) {
isUp = true;
isDown = false;
isLeft = false;
isRight = false;
isMoving = true;
playerY -= delta * 0.1f;
} else if (inputHandler.keyDOWN) {
isDown = true;
isUp = false;
isLeft = false;
isRight = false;
isMoving = true;
playerY += delta * 0.1f;
} else if (inputHandler.keyLEFT) {
isLeft = true;
isUp = false;
isDown = false;
isRight = false;
isMoving = true;
playerX -= delta * 0.1f;
} else if (inputHandler.keyRIGHT) {
isRight = true;
isUp = false;
isDown = false;
isLeft = false;
isMoving = true;
playerX += delta * 0.1f;
}
if (isMoving) {
if (inputHandler.keyUP) {
sprite = runningAnimationUP;
sprite.update(delta);
} else if (inputHandler.keyDOWN) {
sprite = runningAnimationDOWN;
sprite.update(delta);
} else if (inputHandler.keyLEFT) {
sprite = runningAnimationLEFT;
sprite.update(delta);
} else if (inputHandler.keyRIGHT) {
sprite = runningAnimationRIGHT;
sprite.update(delta);
}
} else {
if (isDown) {
sprite = standDOWN;
sprite.update(delta);
} else if (isUp) {
sprite = standUP;
sprite.update(delta);
} else if (isLeft) {
sprite = standLEFT;
sprite.update(delta);
} else if (isRight) {
sprite = standRIGHT;
sprite.update(delta);
}
}
// Map Boundaries
if (playerX > mapX) {
playerX = mapX;
} else if (playerX < 0) {
playerX = 0;
}
if (playerY > mapY) {
playerY = mapY;
} else if (playerY < 0) {
playerY = 0;
}
camera.update(playerX, playerY, mapX, mapY);
}
@Override
public void render(GameContainer gameContainer, StateBasedGame stateBasedGame, Graphics graphics) throws SlickException {
testMap.draw(mapX - 1024, mapY - 1024);
graphics.translate(camera.getCamX(), camera.getCamY());
sprite.draw(playerX, playerY);
graphics.translate(-camera.getCamX(), -camera.getCamY());
graphics.drawString("Player X: " + playerX, 400, 400);
graphics.drawString("Player Y: " + playerY, 400, 415);
}
我觉得我的 Camera.class 和我的 update(...) & render(...) 方法有问题,但我不知道是什么。
这里在你的相机class更新方法中尝试改变
camX = playerX - Main.WIDTH/2;
camY = playerY - Main.HEIGHT/2;
至
camX = playerX;
camY = playerY;
如果这不起作用,请告诉我。