Search code examples
javaandroidlibgdxscene2d

Libgdx scene2d inputprocessor not working?


So basically all I am trying to achieve here is very simple. Load up the game with the menu screen. Click the play button, moves to a "play screen", character dies, "home button" clicked goes back to the menu screen. However, when I try to click on the "play button" after my character dies, it does not seem to register the clicks at all. Bear in mind that on the first attempt (when the app is first opened) my buttons in the main menu class are clickable. However after i try to transition back into the same main menu class using the method updateGOSButtons() as shown below, the buttons become unclickable.

MENU SCREEN CLASS

public void show() {
        //init table and stuff
        buttonPlay.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                buttonClicked.play();
                game.setScreen(new GameScreen(game));
                dispose();
            }
        });

        buttonRate.addListener(new ClickListener() {
            @Override
            public void clicked(InputEvent event, float x, float y) {
                buttonClicked.play();
                System.out.println("Nothing happens Yet");
            }
        });
        Gdx.input.setInputProcessor(stage);
}

PLAY SCREEN CLASS

public void updateGOSButtons() {
        if (gos.isGoButtonClicked()) {
            gameOver.stop();
            backgroundMusic.play();

            gameState = GameState.START;

            dispose();
            game.setScreen(new GameScreen(game));
        }

        if (gos.isHomeButtonClicked()) {
            gameOver.stop();
            backgroundMusic.play();

            gameState = GameState.START;

            dispose();
            game.setScreen(new MainMenuScreen(game));
        }

        Gdx.input.setInputProcessor(gos.getStage());
    }

Solution

  • I believe you are calling the updateGOSButtons method in the render of GameScreen. I think that the problem can be that after setting screen to MainScreen

        if (gos.isHomeButtonClicked())
        {
            ...
    

    the

        Gdx.input.setInputProcessor(gos.getStage());
    

    is called one more time and "overrides" the

        Gdx.input.setInputProcessor(stage);
    

    from MainMenu screen which is called only once (in show method so at the start of the screen).

    The solution is to move te setting of inputProcessor in a gameScreen to the show() method (so it will be called only once).

    If you for some reason need more than one stage to be inputProcessor just use InputMultiplexer - you can read how to use it here:

    https://github.com/libgdx/libgdx/wiki/Event-handling