Search code examples
libgdxconceptual

Gdx.input.setInputProcessor for several Screens. (LIBGDX, Conceptional)


I have:

  • one Main ApplicationListener
  • several screens which I set with setScreen(screen) in my Main depending on the context.
  • each screen has their stages with their Inputlisteners.

Where and how do I set the Gdx.input.setInputProcessor for the different Screens ?

I mean when switching the screen I have to unset all listeners of the old screen and add the new ones from the new screen, so they do not overlap. I cant do this in the constructor of the screen, because it is called only ones. I could do it in the @Override.resize method of the screen, but I guess this is not the way I should do ?

Where should I do this ?


Solution

  • You should call Gdx.input.setInputProcessor() in the show() method.

    Because screen.show() will call everytime you call setScreen(screen);

    And you don't need to remove the old one because the function Gdx.input.setInputProcessor() is simple like that:

    @Override
    public void setInputProcessor (InputProcessor processor) {
        this.processor = processor;
    }
    

    So it replaces the old one.