1
\$\begingroup\$

I'm just getting started with libGDX and have run into a snag registering an InputListener for a button. I've gone through many examples and this code appears correct to me but the associated callback never triggers ("touched" is not printed to console). I'm just posting the code with the abstract game screen and the implementing screen. The application starts successfully with a label of "Exit" in the bottom left hand corner, but clicking the button/label does nothing.

I'm guessing the fix is something simple. What am I overlooking?

public abstract class GameScreen<T> implements Screen
{
    protected final T game;
    protected final SpriteBatch batch;
    protected final Stage stage;

    public GameScreen(T game) 
    {
        this.game = game;
        this.batch = new SpriteBatch();
        this.stage = new Stage(0, 0, true);
    }

    @Override
    public final void render(float delta) 
    {
        update(delta);

        // Clear the screen with the given RGB color (black)
        Gdx.gl.glClearColor(0f, 0f, 0f, 1f);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT);

        stage.act(delta);
        stage.draw();
    }

    public abstract void update(float delta);

    @Override
    public void resize(int width, int height) 
    {
        stage.setViewport(width, height, true);
    }

    @Override
    public void show() 
    {
        Gdx.input.setInputProcessor(stage);
    }

    // hide, pause, resume, dipose
}

public class ExampleScreen extends GameScreen<MyGame>
{
    private TextButton exitButton;

    public ExampleScreen(MyGame game)
    {
        super(game);
    }

    @Override
    public void show()
    {
        super.show();

        TextButton.TextButtonStyle buttonStyle = new TextButton.TextButtonStyle();          
        buttonStyle.font = Font.getFont("Origicide", 32);
        buttonStyle.fontColor = Color.WHITE;

        exitButton = new TextButton("Exit", buttonStyle);
        exitButton.addListener(new InputListener() {
            @Override
            public void touchUp (InputEvent event, float x, float y, int pointer, int button) {
                System.out.println("touched");
            }
        });
        stage.addActor(exitButton);
    }

    @Override
    public void update(float delta) 
    {    
    }
}
\$\endgroup\$
2
  • \$\begingroup\$ Have you tried also handling the touch down event and returning true? \$\endgroup\$ Commented Aug 1, 2013 at 22:23
  • \$\begingroup\$ Funny you should ask, someone in libGDX chat just suggested that and it worked. I posted their answer below. \$\endgroup\$
    – JPRO
    Commented Aug 1, 2013 at 22:51

1 Answer 1

2
\$\begingroup\$

The solution to this was answered by a kind user in the libGDX chatroom.

In order for touchUp to be triggered, touchDown must also be implemented.

exitButton.addListener(new InputListener() 
{
    @Override
    public bool touchDown (InputEvent event, float x, float y, int pointer, int button) 
    {
        System.out.println("touchdown");
        return true;
    }
    @Override
    public void touchUp (InputEvent event, float x, float y, int pointer, int button) 
    {
        System.out.println("touchup");
    }
});
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .