Skip to main content
Removed an irrelevant tag.
Link
Vaillancourt
  • 16.4k
  • 17
  • 55
  • 61
added 4 characters in body
Source Link
Lynob
  • 381
  • 2
  • 8
  • 26

getMusicPlaying is set to false whenever someone touches the button, but libgdx runs show method only once, at the start of the game, hence the music will keep playing forever.

I tried to use the render and batch.begin instead of show() same result.

private boolean musicPlaying = true;

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

}
});
}

public void show() {
 if (getMusicPlaying ==true)
  music.play();
}

getMusicPlaying is set to false whenever someone touches the button, but libgdx runs show method only once, at the start of the game, hence the music will keep playing forever.

I tried to use the render and batch.begin instead of show() same result.

private boolean musicPlaying = true;

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

}
});
}

public void show() {
 if (getMusicPlaying ==true)
  music.play();
}

getMusicPlaying is set to false whenever someone touches the button, but libgdx runs show method only once, at the start of the game, hence the music will keep playing forever.

I tried to use the render and batch.begin instead of show() same result.

getMusicPlaying is set to false whenever someone touches the button, but libgdx runs show method only once, at the start of the game, hence the music will keep playing forever.

I tried to use the render and batch.begin instead of show() same result.

private boolean musicPlaying = true;

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

}
});
}

public void show() {
 if (getMusicPlaying ==true)
  music.play();
}
deleted 949 characters in body; edited title
Source Link
Lynob
  • 381
  • 2
  • 8
  • 26

Libgdx: How to implement play music/mute option and save it in preferenceupdate void show?

skin.addRegions(buttonsAtlas);
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = skin.getDrawable("muteMusic.up");
textButtonStyle.checked =private skin.getDrawable("music.down");
TextButtonboolean buttonmusicPlaying = new TextButton("", textButtonStyle);
dialog.text("Do you want to play music");
dialog.getContentTable().row();
dialog.getContentTable().add(mBtn).left();
dialog.getContentTable().add(button).right();
dialog.getContentTable().row();
dialog.button("Ok", false).center();true;
dialog.show(stage);
button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
 prefs.putBoolean("music", true);
 Gdx.app.log("music", "Released");
 return true;
} 

public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
 prefs.putBoolean("music", false);
 Gdx.app.logsetMusicplaying("my app", "Released"!musicplaying);
}
}); 

}
});
  • How to save the state of the button in the prefs and display it? (Whether it's pressed or not)
  • Gdx.app.log is correct but if I do boolean musicPlaying = prefs.getBoolean("music") it doesn't work.

basically:

@Override}

public void show() {
boolean musicPlaying =if prefs.getBoolean("music"getMusicPlaying ==true);
if (musicPlaying==true) {
playMusicmusic.play();}
prefs.flush();}

getMusicPlaying is not affected by what's in prefs. I even tried without prefsset to false whenever someone touches the button, using getter and setterbut libgdx runs show method only once, it's not affecting whetherat the music plays or not. Even after restartingstart of the game, hence the music preferences are not loaded correctlywill keep playing forever.

I tried to use the render and batch.begin instead of show() same result.

Libgdx: How to implement play music/mute option and save it in preference?

skin.addRegions(buttonsAtlas);
TextButton.TextButtonStyle textButtonStyle = new TextButton.TextButtonStyle();
textButtonStyle.font = font;
textButtonStyle.up = skin.getDrawable("muteMusic.up");
textButtonStyle.checked = skin.getDrawable("music.down");
TextButton button = new TextButton("", textButtonStyle);
dialog.text("Do you want to play music");
dialog.getContentTable().row();
dialog.getContentTable().add(mBtn).left();
dialog.getContentTable().add(button).right();
dialog.getContentTable().row();
dialog.button("Ok", false).center();
dialog.show(stage);
button.addListener(new InputListener() {
public boolean touchDown(InputEvent event, float x, float y, int pointer, int button) {
 prefs.putBoolean("music", true);
 Gdx.app.log("music", "Released");
 return true;
}
public void touchUp(InputEvent event, float x, float y, int pointer, int button) {
 prefs.putBoolean("music", false);
 Gdx.app.log("my app", "Released");
}
});
}
});
  • How to save the state of the button in the prefs and display it? (Whether it's pressed or not)
  • Gdx.app.log is correct but if I do boolean musicPlaying = prefs.getBoolean("music") it doesn't work.

basically:

@Override
public void show() {
boolean musicPlaying = prefs.getBoolean("music");
if (musicPlaying==true) {
playMusic.play();}
prefs.flush();}

is not affected by what's in prefs. I even tried without prefs, using getter and setter, it's not affecting whether the music plays or not. Even after restarting the game, the music preferences are not loaded correctly.

How to update void show?

private boolean musicPlaying = true;

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

}
});
}

public void show() {
 if (getMusicPlaying ==true)
  music.play();
}

getMusicPlaying is set to false whenever someone touches the button, but libgdx runs show method only once, at the start of the game, hence the music will keep playing forever.

I tried to use the render and batch.begin instead of show() same result.

added 14 characters in body
Source Link
Lynob
  • 381
  • 2
  • 8
  • 26
Loading
deleted 3 characters in body
Source Link
Lynob
  • 381
  • 2
  • 8
  • 26
Loading
Source Link
Lynob
  • 381
  • 2
  • 8
  • 26
Loading