Vahe Muradyan
Vahe Muradyan

Reputation: 1115

Android : LibGDX button

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.

Upvotes: 1

Views: 460

Answers (1)

Alon Zilberman
Alon Zilberman

Reputation: 2155

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

Upvotes: 3

Related Questions