1

How can I create LibGDX button and set touch listener to it?I want to create main menu of my game and put three buttons there (play,settings,about).I tried to do that with Sprite but how I saw Sprite doesnt have touch listener.

asked May 31, 2014 at 6:13

1 Answer 1

3

You can use Stage and Button class, something like this:

 Button btn = new Button(new SpriteDrawable(yourIdleSprite),
 new SpriteDrawable(yourPressedSprite));
 btn.addListener(new ClickListener() {
 public void clicked(InputEvent event, float x, float y) {
 //your code
 }
 });
 Stage stage= new Stage();
 stage.addActor(btn);

There also lots of ways to create your own button class, you can also find a lot of examples in official documentation -> https://github.com/libgdx/libgdx/wiki

answered May 31, 2014 at 21:00
Sign up to request clarification or add additional context in comments.

2 Comments

I have tried in that way but click listener dont work((
post your code here, maybe I will able to help. Also maybe you didn't set inputProcessor? Like this: Gdx.input.setInputProcessor(stage); - you should set inputProcessor in order to handle clicks.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.