LibGDX:点击鼠标时通过按键移动相机会出现延迟
LibGDX: Camera movement by keys gets lag when mouse is clicked
这是我的情况:https://imgur.com/gallery/HNDfwx1
正如您在视频中看到的,每当我在更改相机移动方向后单击鼠标时,游戏都会出现延迟,如果我让相机沿同一方向移动,则只有第一次单击会导致延迟,而后一次单击会导致延迟。这是我的 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;
}
}
这是我的 GameScreen
代码,用于注册 CameraHandler
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);
}
我该如何解决这个问题?谢谢。
问题是我将 Manjaro Linux 与 Xorg 一起使用,而 Xorg 存在输入延迟问题。然后换成Wayland后问题就没有了
这是我的情况:https://imgur.com/gallery/HNDfwx1
正如您在视频中看到的,每当我在更改相机移动方向后单击鼠标时,游戏都会出现延迟,如果我让相机沿同一方向移动,则只有第一次单击会导致延迟,而后一次单击会导致延迟。这是我的 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;
}
}
这是我的 GameScreen
代码,用于注册 CameraHandler
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);
}
我该如何解决这个问题?谢谢。
问题是我将 Manjaro Linux 与 Xorg 一起使用,而 Xorg 存在输入延迟问题。然后换成Wayland后问题就没有了