Detect user input in render() or using InputProcessor in libgdx

梦想与她 提交于 2019-12-11 06:15:08

问题


I am new to libgdx and I wonder if you should use the render() method to get user input or if you should use the InputProcessor.


回答1:


That depends on the use-case. Why do you need it and does that need to be done event driven or continuous?

For example, if you want to move a sprite on the screen while a certain key is pressed, then that's a continuous action:

@Override public void render() {
    if (Gdx.input.isKeyPressed(Input.Keys.SPACE))
        sprite.translateX(speed * Gdx.graphics.getDeltaTime());
    ...
}

However, if you for example want to change the color when the user presses a certain key, then that's an event:

@Override public void create() {
    Gdx.input.setInputProcessor(new InputAdapter() {
        @Override public boolean keyUp (int keycode) {
            if (keycode == Input.Keys.SPACE)
                sprite.setColor(Color.RED);
        }
    });
    ...
}

Note that polling is a convenience method built on top of events, it is very easy to that yourself. Like so:

private boolean moving;
@Override public void create() {
    Gdx.input.setInputProcessor(new InputAdapter() {
        @Override public boolean keyDown (int keycode) {
            if (keycode == Input.Keys.SPACE)
                moving = true;
        }
        @Override public boolean keyUp (int keycode) {
            if (keycode == Input.Keys.SPACE)
                moving = false;
        }
    });
    ...
}

@Override public void render() {
    if (moving)
        sprite.translateX(speed * Gdx.graphics.getDeltaTime());
    ...
}

This often allows you to write more clean and use-case specific code, like so:

private float speed;
@Override public void create() {
    Gdx.input.setInputProcessor(new InputAdapter() {
        @Override public boolean keyDown (int keycode) {
            switch (keycode) {
                case Input.Keys.LEFT: speed -= 10f; break;
                case Input.Keys.RIGHT: speed += 10f; break;
            }
        }
        @Override public boolean keyUp (int keycode) {
            switch (keycode) {
                case Input.Keys.LEFT: speed += 10f; break;
                case Input.Keys.RIGHT: speed -= 10f; break;
            }
        }
    });
    ...
}

@Override public void render() {
    sprite.translateX(speed * Gdx.graphics.getDeltaTime());
    ...
}

With this in mind, it can in many cases be better to use event driven input handling. However, if you find yourself using a lot of boolean flags, then you might as well use the builtin input polling.




回答2:


You should use InputProcessor for user input.

if you want to write anonymously then in show method you should write:-

 Gdx.input.setInputProcessor(new InputProcessor() {

        @Override
        public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean touchDragged(int arg0, int arg1, int arg2) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean scrolled(int arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean mouseMoved(int arg0, int arg1) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyUp(int arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyTyped(char arg0) {
            // TODO Auto-generated method stub
            return false;
        }

        @Override
        public boolean keyDown(int arg0) {
            // TODO Auto-generated method stub
            return false;
        }
    });

or you can implement input processor and register instance in setInputProcessor. Example:-

     public class InputTest implements InputProcessor {

@Override
public boolean keyDown(int arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean keyTyped(char arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean keyUp(int arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean mouseMoved(int arg0, int arg1) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean scrolled(int arg0) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean touchDown(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean touchDragged(int arg0, int arg1, int arg2) {
    // TODO Auto-generated method stub
    return false;
}

@Override
public boolean touchUp(int arg0, int arg1, int arg2, int arg3) {
    // TODO Auto-generated method stub
    return false;
}

}

in show method of your screen class you should write:-

public void show() {

    Gdx.input.setInputProcessor(new InputTest());
}

in my suggestion these two are the best way to take user input if you are not using stage. Hope this will work for you.



来源:https://stackoverflow.com/questions/39337922/detect-user-input-in-render-or-using-inputprocessor-in-libgdx

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