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

Here is 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 create a simple game, and that is how I create the main menu. I want to make my startImage tangible (clickable) since there is no listener available for the Image class. How can I do it? Or is it better to use ImageButton? Thus, I would need to create an image atlas, which for me is now a little more complicated.

EDIT: I tried the 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); 

need to call. Solvable.

+6
source share
1 answer

The solution is as follows:

 Gdx.input.setInputProcessor(stage); 
+9
source

All Articles