【问题标题】:How to change imagebutton when clicking on it?单击图像按钮时如何更改图像按钮?
【发布时间】:2019-05-01 19:10:26
【问题描述】:

我正在尝试在 LibGDX 中制作一个声音系统,您可以在其中按下一个图像按钮,然后该图像按钮变成另一个图标,例如打开和关闭声音标志。我不知道如何制作它,因为一旦我为 imagebutton 创建一个新的构造函数,它就会丢失监听器内部的代码。我觉得这里需要使用某种循环,并试图让它在循环内工作,但似乎不起作用。

这是我的代码:

musicBtn.addListener(new ChangeListener() { //musicBtn is the imageButton


               @Override
               public void changed(ChangeEvent event, Actor actor) {

                   if(GameManager.getInstance().gameData.isMusicOn()){//if the music is on and you press the music button then this turns it off
                       GameManager.getInstance().gameData.setMusicOn(false);
                       GameManager.getInstance().stopMusic();


                   } else {//if the music is off and players presses music button it turns on
                       GameManager.getInstance().gameData.setMusicOn(true);
                       GameManager.getInstance().playMusic();

                   }

                   GameManager.getInstance().saveData(); //method that saves the data

               }
           });

【问题讨论】:

    标签: libgdx imagebutton


    【解决方案1】:

    您可以将 Drawable 传递给名为 imageChecked 的 ImageButton 构造函数:

    ImageButton (Drawable imageUp, Drawable imageDown, Drawable imageChecked)
    

    例如,这可能是一个关闭声音的标志。每当你按下按钮时,检查音乐是否正在播放,如果设置为 true,这会将图像更改为声音关闭标志,否则将其设置为 false,它会变回原来的样子

    if(GameManager.getInstance().gameData.isMusicOn()){
         ...
         musicBtn.setChecked(true)
    }else{
        ...
        musicBtn.setChecked(false)
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-11-05
      • 2013-07-24
      • 1970-01-01
      • 2017-01-21
      • 2012-10-07
      • 1970-01-01
      • 2018-04-14
      相关资源
      最近更新 更多