Home » Android » android – What is the simplest way to make Image touchable in libgdx?

android – What is the simplest way to make Image touchable in libgdx?

Posted by: admin June 15, 2020 Leave a comment

Questions:

Here’s my code:

@Override
public void resize(int width, int height) {
super.resize(width, height);

stage.clear();

// background image
Image backImage = new Image(backTexture);
backImage.setX(0);
backImage.setY(0);
backImage.setWidth(800);
backImage.setHeight(480);

// start game image
Image startImage = new Image(startTexture);
startImage.setX(415);
startImage.setY(180);
startImage.setWidth(323);
startImage.setHeight(69);

stage.addActor(backImage);
stage.addActor(startImage);
}

I’m creating simple game and this is the way I’m creating main menu. I want to make my startImage touchable (clickable) as there is no available listener for Image class. How should I do this? Or is it better to use ImageButton? This way I would have to create image atlas which seems to be a little bit complicated for me now..

EDIT:
I tried following:

startImage.addListener(new ClickListener() {
    public boolean touchDown(InputEvent event, float x, float y, int pointer, int button)
    {
        game.setScreen(new SplashScreen(game));
        return true;
    }
});

But it still doesn’t work..

EDIT:

Gdx.input.setInputProcessor(stage);

needs to be called. Solved.

How to&Answers:

Solution is to call following:

Gdx.input.setInputProcessor(stage);