【发布时间】:2016-08-29 12:31:43
【问题描述】:
我最近开始使用 libgdx 进行编码,并构建了一个小小的“在世界中四处走动”游戏。问题是向上和向左(W 和 A)比向右和向下更快。 W,A 和 D,S 的代码没有区别。 这是我的输入处理代码,它在“渲染”之后的每一帧都被调用:
public void processInput() {
float delta = Gdx.graphics.getDeltaTime();
System.out.println(1.0/delta);
if (Gdx.input.isKeyJustPressed(Keys.A)) {
player.posX -= 100 * delta;
player.startAnimation(0); // Animation 0 = LEFT
elapsedTime = 0;
} else if (Gdx.input.isKeyJustPressed(Keys.D)) {
player.posX += 100 * delta;
player.startAnimation(1); // Animation 1 = RIGHT
elapsedTime = 0;
}
if (Gdx.input.isKeyJustPressed(Keys.W)) {
player.posY -= 100 * delta;
player.startAnimation(2); // Animation 2 = BACK
elapsedTime = 0;
} else if (Gdx.input.isKeyJustPressed(Keys.S)) {
player.posY += 100 * delta;
player.startAnimation(3); // Animation 3 = FRONT
elapsedTime = 0;
}
if(Gdx.input.isKeyJustPressed(Keys.ESCAPE)){
Gdx.app.exit();
}
boolean pressed = false;
if (Gdx.input.isKeyPressed(Keys.A)) {
player.posX -= 100 * delta;
pressed = true;
} else if (Gdx.input.isKeyPressed(Keys.D)) {
player.posX += 100 * delta;
pressed = true;
}
if (Gdx.input.isKeyPressed(Keys.W)) {
player.posY -= 100 * delta;
pressed = true;
} else if (Gdx.input.isKeyPressed(Keys.S)) {
player.posY += 100 * delta;
pressed = true;
}
if (!pressed) {
player.stopAnimation();
}
if(player.posX < 0){
player.posX = 0;
}
if(player.posY < 0){
player.posY = 0;
}
if(player.posX > world.sizeX()*50){
player.posX = world.sizeX()*50;
}
if(player.posY > world.sizeY()*50){
player.posY = world.sizeY()*50;;
}
player.update(delta);
}
这里还有一个包含完整代码的 Dropbox 链接:https://www.dropbox.com/sh/p8umkyiyd663now/AAC4zt716rII-8sQpEbfuNuZa?dl=0
【问题讨论】:
-
尝试在完美的方形窗口中运行游戏,看看您是否遇到相同的行为? stackoverflow.com/questions/21079122/…
-
是的,先生。方形窗户也是如此。
-
你相机的视口也是一个完美的正方形吗?
-
是的,我只是将 Gdx.graphics.getWidth/getHeight 作为相机视口。
-
我想你认为这个问题是屏幕分辨率的拉伸,这可以用一个完美的正方形来解决,但我可以看到玩家位置的 X 和 Y 值变化得更快而不仅仅是更快的运动。