GestureDetector and InputListener in libgdx

人盡茶涼 提交于 2019-12-10 15:20:32

问题


I'm new to libgdx and what I'm trying to achieve here is that I want my GestureDetector to work at the same time with InputListener. I have two buttons on the left and I need them to keep reacting even if I start swiping at the same time(multitouch). I used InputMultiplexer but it doesn't work that way I need. I checked all the return values of InputListener and GestureDetector and everything that I need returns true, also GestureInputListener is a class that implements GestureDetector.GestureListener. I only used fling in it. Both GestureInputListener and InputListener work but not at the same time. Could you help me with that? links, ideas. Thanks. Code below:

inputMultiplexer.addProcessor(stage);
inputMultiplexer.addProcessor(new GestureDetector(gestureInputListener));
Gdx.input.setInputProcessor(inputMultiplexer); 

回答1:


then instead of using input multiplexer use code as:

public class MyGestureDetector extends GestureDetector {
    public MyGestureDetector(GestureListener listener) {
        super(listener);
    }

    @Override
    public boolean touchUp(float x, float y, int pointer, int button) {
        super.touchUp(x, y, pointer, button);

        // Your Code Here
        return true;
    }
}

similarly other functions such as touchdown etc can be added here don't forget to call super from functions as this will make various function like fling and tap work

Edit:

change the implementation to

public class MyGestureDetector extends GestureDetector {
    private Stage stage;
    public MyGestureDetector(GestureListener listener,Stage stage) {
        super(listener);
        this.stage = stage;
    }


    @Override
    public boolean keyDown(int keycode) {
        stage.keyDown(keycode);
        super.keyDown(keycode);
        return false;
    }

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

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

    @Override
    public boolean touchDown(int screenX, int screenY, int pointer, int button) {
        stage.touchDown(screenX, screenY, pointer, button);
        super.touchDown(screenX, screenY, pointer, button);
        return false;
    }

    @Override
    public boolean touchUp(int screenX, int screenY, int pointer, int button) {
        stage.touchUp(screenX, screenY, pointer, button);
        super.touchUp(screenX, screenY, pointer, button);
        return false;
    }

    @Override
    public boolean touchDragged(int screenX, int screenY, int pointer) {
        // TODO Auto-generated method stub
        stage.touchDragged(screenX, screenY, pointer);
        super.touchDragged(screenX, screenY, pointer);

        return false;
    }

    @Override
    public boolean mouseMoved(int screenX, int screenY) {
        stage.mouseMoved(screenX, screenY);
        super.mouseMoved(screenX, screenY);
        return false;
    }

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


}

Here stage is the scene2d Stage and there might be some error of float or int in parameters depending on the libgdx version you are using this is written in 0.9.9 nightlies

P.S.- This implementation is customised as per your problem but it should be tried that every case to be handled by returning true or false as explained before




回答2:


while using any of following classes (GestureDetector or InputProcessor) if you return true from its functions then it doesn't check any other processor attached to your game..

in your code you have added stage first then your gesture Detector if at a time your stage is working and processing inputs and returning true then your gesture detector won't get calls of various functions.

If you want both your stage and gesture detector to work then never return true from any of the function in your gesture listener and stage


InputMultiplexer multiplexer = new InputMultiplexer();
multiplexer.addProcessor(new MyUiInputProcessor());
multiplexer.addProcessor(new MyGameInputProcessor());
Gdx.input.setInputProcessor(multiplexer);

The InputMultiplexer will hand any new events to the first InputProcessor that was added to it. If that processor returns false from the method invoked to handle the event, this indicates the event was not handled and the multiplexer will hand the event to the next processor in the chain. Through this mechanism, the MyUiInputProcessor can handle any events that fall inside one of its widgets and pass on any other events to the MyGameInputProcessor.

Libgdx Wiki InputEvent




回答3:


ActorGestureListener help in some cases:

        this.ball.addListener(new ActorGestureListener() {
            public void touchDown(InputEvent event, float x, float y, int pointer, int button) {
            }
            public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
            }
            public void tap(InputEvent event, float x, float y, int count, int button) {
            }
            public boolean longPress(Actor actor, float x, float y) {
                return false;
            }
            public void fling(InputEvent event, float velocityX, float velocityY, int button) {
            }
            public void pan(InputEvent event, float x, float y, float deltaX, float deltaY) {
            }
            public void zoom(InputEvent event, float initialDistance, float distance) {
            }
            public void pinch(InputEvent event, Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
            }
        });

Where "ball" is an Actor.



来源:https://stackoverflow.com/questions/18559583/gesturedetector-and-inputlistener-in-libgdx

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