GestureDetector in libgdx

I am new to libgdx. I have the following problem. I want to do the scaling for my game. For this, I have a GestureDetectorListener class in my GameStage .

In GameStage , I have the following code for the GestureDetector :

 OrthographicCamera camera = new OrthographicCamera(Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); this.setCamera(camera); GestureDetector gestureDetector = new GestureDetector(20, 0.5f, 2, 0.15f, new GestureDetectorListener()); Gdx.input.setInputProcessor(gestureDetector); class GestureDetectorListener implements GestureListener { float initialScale = 1; @Override public boolean touchDown(float x, float y, int pointer, int button) { initialScale = camera.zoom; return true; } @Override public boolean tap(float x, float y, int count, int button) { // TODO Auto-generated method stub return true; } @Override public boolean longPress(float x, float y) { // TODO Auto-generated method stub return true; } @Override public boolean fling(float velocityX, float velocityY, int button) { // TODO Auto-generated method stub return true; } @Override public boolean pan(float x, float y, float deltaX, float deltaY) { return true; } @Override public boolean zoom(float initialDistance, float distance) { float ratio = initialDistance / distance; camera.zoom = initialScale * ratio; return true; } @Override public boolean pinch(Vector2 initialPointer1, Vector2 initialPointer2, Vector2 pointer1, Vector2 pointer2) { // TODO Auto-generated method stub return true; } } 

I also set an InputListener for the Actors of my scene to drag and drop. The problem is that when setting the InputProcessor to the GestureDetector dragging and dropping my Actors does not work. I want to do the zoom using the camera and GestureDetector , but this will not affect the drag and drop. How can i do this?

Any help please! Thanks.

+7
source share
2 answers

CodeNoob is on the right track, but I think you can structure things differently.

You can use InputMultiplexer to set both the global GestureListener and your scene listener:

 GestureDetector gd = ... Stage myStage = ... InputMultiplexer im = new InputMultiplexer(gd, myStage); // Order matters here! Gdx.input.setInputProcessor(im); 

Another thing to note is the return values ​​for all the boolean methods in your input listeners. Anyone returning true evaluates the event as "processed", and the multiplexer does not pass the event to its peer. In addition, the order of the input processors in the multiplexer matters (since it can hide events from the underlying input processor). Thus, automatically generated stubs that return β€œtrue” in your example will β€œeat up” a bunch of events. If this handler is the first in the multiplexer, it will hide events from the scene.

+8
source

You need to create InputMultiplexer InputMultiplexer myInputMultiplexer = new InputMultiplexer()

Then you add your GestureDetector and InputListener to InputMultiplexer ( myInputMultiplexer.addProcessor() ).

And at the end you should set the multiplexer as the main InputProcessor as follows: Gdx.input.setInputProcessor(myInputMultiplexer)

+3
source

All Articles