Valentin
Valentin

Reputation: 280

How to approach Inputprocessing

I am currently trying to build an Options menu in Libgdx using scene2d and im trying to find a way how to approach User input.

I want to implement going back to the mainmenu when the back key on android is pressed, but i am not sure how to do it, since the rest of the input (pressing a button ) would be handled by my stage.

If you have any idea on how to approach this issue and how i could handle the user input correctly please respond. I did not include any code since this is more about how to approach it, instead of an actual problem in my code.

Thanks, Valentin

Upvotes: 0

Views: 30

Answers (1)

shadowcookie
shadowcookie

Reputation: 41

You need to create an InputProcessor and implement its methods, there is no way around that because the back key is separate from the Stage. You also need to have a way to access the main menu object from the InputProcessor; assuming you are using Game and Screens, this is one way of doing it:

public class OptionsScreen implements Screen, InputProcessor {
    final MyGame game;
    public OptionsScreen(MyGame game) { this.game = game; }
    ...
    @Override
    public boolean keyUp(int keycode) {
        if(keycode == Keys.BACK) {
            game.setScreen(game.mainScreen);
            return true;
        }
        return false;
    }
    ...
}

Where game.mainScreen is the Screen of the main menu. It could also be a newly created instance. Of course, you also have to enable the back key before being able to catch it via Gdx.input.setCatchBackKey(true);.

I hope this answers your question.

Upvotes: 1

Related Questions