Libgdx - Check if a key is being held down?

吃可爱长大的小学妹 提交于 2019-12-10 04:00:31

问题


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.

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.


回答1:


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;
    }
}

And using it like this:

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

...

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

You can read more about that here.



来源:https://stackoverflow.com/questions/21464011/libgdx-check-if-a-key-is-being-held-down

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!