Libgdx:点击后更改ImageButton图像

Jov*_*van 2 libgdx

在我在Libgdx的游戏中,我想在点击时更改ImageButton图像.我认为这应该很容易,但我已经浪费了很多时间.:)

    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"));

        }
    }
Run Code Online (Sandbox Code Playgroud)

这不起作用.有人可以帮我弄这个吗?

谢谢

Jov*_*van 10

我通过设置checked图像找到了解决问题的方法

ImageButton (Drawable imageUp, Drawable imageDown, Drawable imageChecked)
Run Code Online (Sandbox Code Playgroud)

然后

    if (gameData.isSound())
        buttonSound.setChecked(false);
    else
        buttonSound.setChecked(true);
Run Code Online (Sandbox Code Playgroud)

  • 当人们回到他们的问题并发布找到的答案时,我真的很感激。 (2认同)