LibGdx: Utilizing a Gesture Listener

2019-07-15 10:17发布

问题:

I would like to use some of the more complex touch screen gestures that you can't access from

Gdx.input

I saw that to do this i must create a Gesture listener so i created the class GestureHandler and copied the code from the wiki. My gesture handler looks like this:

public class GestureHandler implements GestureListener {

@Override
public boolean touchDown(float x, float y, int pointer, int button) {
    return false;
}

@Override
public boolean tap(float x, float y, int count, int button) {
    return false;
}

@Override
public boolean longPress(float x, float y) {
    return false;
}

@Override
public boolean fling(float velocityX, float velocityY, int button) {
    return false;
}

@Override
public boolean pan(float x, float y, float deltaX, float deltaY) {
    return false;
}

@Override
public boolean zoom(float initialDistance, float distance) {
    return false;
}

@Override
public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) {
    return false;
    }   
 }

My question is now that i have set up the gesture listener how can i use it. How can i get the info from these methods? Thank you for any help!

回答1:

From the wiki:

A GestureDetector is an InputProcessor in disguise. To listen for gestures, one has to implement the GestureListener interface and pass it to the constructor of the GestureDetector. The detector is then set as an InputProcessor, either on an InputMultiplexer or as the main InputProcessor

I admit that is rather dense. But a bit farther down on the wiki you'll see:

Gdx.input.setInputProcessor(new GestureDetector(new MyGestureListener()));

To rephrase the above in hopefully less dense English: Your GestureHandler instance is passed to a Libgdx GestureDetector instance. That object will accumulate "raw" inputs and convert them into higher-level "gestures". To get the raw inputs, it needs to be installed where the raw inputs will be delivered to it. The most basic way to install it via Gdx.input.setInputProcessor, but you could also install it via an InputMultiplexer (but that's not worth getting into here).