0
\$\begingroup\$

My game's main game class is a Screen which also implements InputProcessor to handle touch events.

The screen renders a TMX map using an HexagonalMapRenderer and a Camera, like this:

_camera = new OrthographicCamera();
_map = new TmxMapLoader().load("1087.tmx");
_mapRenderer = new HexagonalTiledMapRenderer(_map);
_camera.setToOrtho(false, Gdx.graphics.getWidth(),  Gdx.graphics.getHeight());

To allow the user to scroll the map, I am overriding the touchDown and touchDragged methods; it works.

However, I also have a Scene2D Stage rendering on the Screen, which I use to display some buttons.

They get displayed. and I have created a ClickListener like this:

 button.addListener(new ClickListener(){

    @Override
    public void clicked(InputEvent e, float x, float y){
        System.out.println("Clicked!");
    }
 });

If I use

Gdx.input.setInputProcessor(this);

Only the Screen scrolling functions will work, and the buttons will become unresponsive.

On the other hand, if I use:

    Gdx.input.setInputProcessor(stage);

I will be able to use UI buttons, but the map will become unresponsive.

I'd like to enable both (map will scroll if user clicks anywhere BUT the UI buttons, it will then fire the button clicked listener), is there any way to do this?

Thanks.

\$\endgroup\$
1
\$\begingroup\$

You should use an InputMultiplexer (javadocs | wiki) to combine the two input processors. It works like this:

InputMultiplexer multiplexer = new InputMultiplexer(this, stage);
Gdx.input.setInputProcessor(multiplexer);

This will allow you to use both processors at once. Remember that the return value of the functions are important (true / false)! If once function (let's say touchDragged) return true no other input processors will be called afterwards!

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.