Libgdx - 检查一个键是否被按住?

2022-01-12 00:00:00 cross-platform html java libgdx

我正在使用 java libgdx 游戏库,我很好奇我是否可以判断一个键是否被按住,而不是按下并松开.

I'm using the java libgdx game library and im curious if I can tell if a key is being HELD, not pressed and let go.

我需要知道这一点,因为如果按下它,我将播放一个较短的 mp3 文件,如果按住它,我将播放一个较长的文件.

I need to know this because I'm going to play a shorter mp3 file if it is just pressed and a longer one if it is held.

推荐答案

是的,您可以通过 Gdx.input.isKeyPressed(Input.Keys.XXX) 或通过实现 InputProcessor.

Yes, you can easily check they either via Gdx.input.isKeyPressed(Input.Keys.XXX) or by implementing an InputProcessor.

public class MyInputProcessor implements InputProcessor {

    public boolean keyPressed;

    @Override
    public boolean keyDown(int keycode) {
        if (keycode == Input.Keys.XXX) {
            keyPressed = true;
        }

        return false;
    }

    @Override
    public boolean keyUp(int keycode) {
        if (keycode == Input.Keys.XXX) {
            keyPressed = false;
        }

        return false;
    }
}

并像这样使用它:

MyInputProcessor processor = new MyInputProcessor();
Gdx.input.setInputProcessor(processor);

...

if (processor.keyPressed) {
    // do some stuff
}

您可以在此处了解更多信息.

You can read more about that here.

相关文章