Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

libgdx

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.

like image 797
milanseitler Avatar asked Dec 13 '12 15:12

milanseitler


1 Answers

Solution is to call following:

Gdx.input.setInputProcessor(stage);
like image 115
milanseitler Avatar answered Nov 07 '22 22:11

milanseitler