java - LibGDX:单击鼠标时按键的相机移动会滞后
问题描述
这是我的情况: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后,问题就消失了。
推荐阅读
- c# - 按下按钮后游戏冻结
- mono - 无法使用 mcs 命令将 xUnit 测试类(通过单声道)编译为 exe 文件
- go - 查询现有表返回空结果
- python - TypeError: 'str' object is not callable error while print the text from a div tag using Selenium and Python
- r - 从谷歌驱动器将 .por 数据集读入 R
- python - 如果重复超过 n 次,则删除 Pandas 数据框中的连续重复项
- python - Pyspark - 如何分别初始化常见的 DataFrameReader 选项?
- php - 谷歌云平台wordpress选择PHP版本
- python - 尝试在二叉搜索树中实现重新平衡方法,在方法内部获取 lyst 中间时遇到问题
- javascript - 使用 NodeJS 和 Axios 遍历 Mongo 集合