Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Libgdx: change ImageButton image on click

Tags:

libgdx

In my game in Libgdx I want to change ImageButton image on click. I think this should be easy, but I have lost hours on this. :)

    public void show() {
        buttonSound = new ImageButton(skin.getDrawable("sound_off"));
        buttonSound.addListener(new onSoundListener());
}

    class  onSoundListener extends InputListener {

        public boolean touchDown (InputEvent event, float x, float y, int pointer, int button) {

            return true;
        }

        public void touchUp (InputEvent event, float x, float y, int pointer, int button) {

            buttonSound.setBackground(skin.getDrawable("btn_sound"));

        }
    }

This doesn't work. Can someone help me with this?

Thanks

like image 307
Jovan Avatar asked Dec 08 '22 05:12

Jovan


1 Answers

I found solution for my problem by setting checked image

ImageButton (Drawable imageUp, Drawable imageDown, Drawable imageChecked)

and then

    if (gameData.isSound())
        buttonSound.setChecked(false);
    else
        buttonSound.setChecked(true);
like image 177
Jovan Avatar answered Jan 11 '23 14:01

Jovan