0
\$\begingroup\$

I'm saving music in libgdx preference, the following code works perfectly but if you enter the settings again you'd see the initial button up regardless if the sound is muted or not.

TextureAtlas buttonsAtlas = new TextureAtlas("ui/dialogButtons.pack");
textButtonStyle.up = skin.getDrawable("music.down");
textButtonStyle.checked = skin.getDrawable("muteMusic.up");
button.addListener(new 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) {
  setMusicplaying(!musicplaying);
  if (getMusicplaying() == false)
   playMusic.stop();
  else playMusic.play();
 }
});

I tried to do this, it works perfecly but doesn't switch the button, when the user is clicking on it, it only switches when the user closes the dialog

if (getMusicplaying() == true) {
textButtonStyle.up = skin.getDrawable("music.down");
textButtonStyle.down = skin.getDrawable("muteMusic.up");}
if (getMusicplaying() == false) {
  textButtonStyle.up = skin.getDrawable("muteMusic.up");
  textButtonStyle.down = skin.getDrawable("music.down");}
\$\endgroup\$
  • \$\begingroup\$ @AlexandreVaillancourt good to know, just did \$\endgroup\$ – Lynob Feb 8 '17 at 13:17
  • \$\begingroup\$ github.com/libgdx/libgdx/wiki/Preferences ? \$\endgroup\$ – realUser404 Feb 8 '17 at 13:18
  • \$\begingroup\$ @realUser404 yes that but what I don't know how to do, is how to save the pressed button state. right now, when I enter the settings, the button shows in its original state, whether it's pressed or not \$\endgroup\$ – Lynob Feb 8 '17 at 13:23
  • \$\begingroup\$ @AlexandreVaillancourt great tip! i can see it makes great difference, my previous question was the opposite and it makes it unreadable. wish i knew it before! thanks. \$\endgroup\$ – Lynob Feb 8 '17 at 13:25
0
\$\begingroup\$

I'm posting this answer since it may help someone, i got it from libgdx forums

if (getMusicplaying() == false)
 button.setChecked(true) 

no need for more code, that's it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.