SkR
SkR

Reputation: 102

LibGdx : Using setInputProcessor(stage) in different classes

I am developing android game using LibGdx; for the moment, there are 2 menu screens with several buttons, a mainMenu and a gameOverMenu. I need to use Gdx.input.setInputProcessor(stage); in both classes but i can't... There's only one screen working.

I search a lot on the forum and i found only Gdx.input.setInputProcessor(inputMultiplexer)...

InputProcessor inputProcessorOne = new CustomInputProcessorOne();
InputProcessor inputProcessorTwo = new CustomInputProcessorTwo();
InputMultiplexer inputMultiplexer = new InputMultiplexer();
inputMultiplexer.addProcessor(inputProcessorOne);
inputMultiplexer.addProcessor(inputProcessorTwo);
Gdx.input.setInputProcessor(inputMultiplexer);

I'm having trouble with it because it's used in order to have several processor in the same class.

But i just need 1 processor in two separate classes.

For the moment, i have this... But it doesn't work:

CLASS 1 Gdx.input.setInputProcessor(stageMainMenu);

CLASS 2 Gdx.input.setInputProcessor(stageGameOverMenu);

EDIT : @MennoGouw the problem, with what i quote, is that i'd like to put Stage Object in addProcessor, not InputProcessor. I don't want to create my own Processor, i'd use ClickListener. Besides i don't really need to use both processors at the same time. Indeed, these are two different screens. However, there is no clear method for InputProcessor.

Besides i don't know where i should build InputMultiplexer .

I'll try to be more clear, I have : MyGame.java with only one method ( public void create() { mainMenuScreen = new MainMenu(this); //gameScreen = new MainGame(this); gameOverScreen = new GameOver(this); setScreen(mainMenuScreen);
}
)

And GameOver.java & MainMenu.java. (the two screens) If i put Gdx.input.setInputProcessor(stageName) in the constructor, only one of the two runs. Else, in the show() method, it crashes.

Thanks for your time

Upvotes: 3

Views: 3070

Answers (1)

Marco Klein
Marco Klein

Reputation: 311

What's the issue with InputMultiplexer? It is made for what you are looking for.

You could also use this piece of code to add the processers in each Stage respectively:

First setup Multiplexer in your init Game class function:

Gdx.input.setInputProcessor(new InputMultiplexer());

And later check if you have to add your input processor:

InputMultiplexer inputMultiplexer = (InputMultiplexer) Gdx.input.getInputProcessor();
if (!inputMultiplexer.getProcessors().contains(stageMainMenu))
    inputMultiplexer.addProcessor(stageMainMenu);

Upvotes: 2

Related Questions