LibGDX:当点击鼠标时,通过键移动相机会延迟



这是我的情况:https://i.stack.imgur.com/Xjv99.jpg

正如你在视频中所看到的,每当我在改变摄像机移动方向后点击鼠标时,游戏就会出现延迟,如果我保持摄像机在同一方向移动,那么只有第一次点击才会导致延迟,而之后的点击则会导致延迟。这是我的CameraHandler代码:

public CameraMovementHandler(GameScreen screen) {
super(screen);
pressedButton = 0b0000;
map = screen.getMap();
camera = screen.getCamera();
desiredPosition = camera.position;
}
public void update() {
if ((pressedButton & 0b0010) != 0) {
desiredPosition.x += CAMERA_SPEED * Gdx.graphics.getDeltaTime();
}
if ((pressedButton & 0b1000) != 0) {
desiredPosition.x -= CAMERA_SPEED * Gdx.graphics.getDeltaTime();
}
if ((pressedButton & 0b0001) != 0) {
desiredPosition.y += CAMERA_SPEED * Gdx.graphics.getDeltaTime();
}
if ((pressedButton & 0b0100) != 0) {
desiredPosition.y -= CAMERA_SPEED * Gdx.graphics.getDeltaTime();
}
camera.position.lerp(desiredPosition, 0.1f);
camera.position.x = MathUtils.clamp(camera.position.x, GameScreen.VIEWPORT_WIDTH / 2.f,
map.getWidth() - GameScreen.VIEWPORT_WIDTH / 2.f);
camera.position.y = MathUtils.clamp(camera.position.y, GameScreen.VIEWPORT_HEIGHT / 2.f,
map.getHeight() - GameScreen.VIEWPORT_HEIGHT / 2.f);
camera.update();
}
@Override
public boolean keyDown(int keycode) {
catchPressedButton(keycode);
return false;
}
@Override
public boolean keyUp(int keycode) {
catchPressedButton(keycode);
return false;
}
private void catchPressedButton(int keycode) {
if (keycode == Input.Keys.A) {
pressedButton ^= 0b1000;
} else if (keycode == Input.Keys.S) {
pressedButton ^= 0b0100;
} else if (keycode == Input.Keys.D) {
pressedButton ^= 0b0010;
} else if (keycode == Input.Keys.W) {
pressedButton ^= 0b0001;
}
}

这是注册CameraHandler

GameScreen代码
public GameScreen(UntitledFarmGame untitledFarmGame) {
this.untitledFarmGame = untitledFarmGame;
inputMultiplexer = new InputMultiplexer();
spriteBatch = new SpriteBatch();
map = new Map(100, 100, spriteBatch, untitledFarmGame.getResourceManager());
camera = new OrthographicCamera();
viewport = new ExtendViewport(VIEWPORT_WIDTH, VIEWPORT_HEIGHT, camera);
gameHud = new GameHud(this, untitledFarmGame.getResourceManager());
}
public void addPlayerInputHandler(PlayerInputHandler inputHandler) {
inputMultiplexer.removeProcessor(inputHandler);
inputMultiplexer.addProcessor(inputHandler);
}
public void removePlayerInputHandler(PlayerInputHandler inputHandler) {
inputMultiplexer.removeProcessor(inputHandler);
}
@Override
public void show() {
inputMultiplexer.addProcessor(new CameraMovementHandler(this));
inputMultiplexer.addProcessor(gameHud);
Gdx.input.setInputProcessor(inputMultiplexer);
}
@Override
public void render(float delta) {
ScreenUtils.clear(0, 0, 0, 1);
viewport.apply();
spriteBatch.setProjectionMatrix(camera.combined);
map.draw();
inputMultiplexer.getProcessors().forEach(i -> {
if (i instanceof PlayerInputHandler) {
((PlayerInputHandler) i).update();
}
});
gameHud.getViewport().apply();
gameHud.act(delta);
gameHud.draw();
}
@Override
public void resize(int width, int height) {
viewport.update(width, height);
camera.setToOrtho(false, width, height);
gameHud.getViewport().update(width, height);
}

如何解决这个问题?谢谢你。

问题是我在Xorg上使用Manjaro Linux, Xorg有输入延迟问题。然后切换到韦兰,问题就解决了。

相关内容

  • 没有找到相关文章

最新更新