Game Development Reference
In-Depth Information
Create a new file called GamePreferences and add the following lines of code:
package com.packtpub.libgdx.canyonbunny.util;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Preferences;
import com.badlogic.gdx.math.MathUtils;
public class GamePreferences {
public static final String TAG =
GamePreferences.class.getName();
public static final GamePreferences instance =
new GamePreferences();
public boolean sound;
public boolean music;
public float volSound;
public float volMusic;
public int charSkin;
public boolean showFpsCounter;
private Preferences prefs;
// singleton: prevent instantiation from other classes
private GamePreferences () {
prefs = Gdx.app.getPreferences(Constants.PREFERENCES);
}
public void load () { }
public void save () { }
}
This class is implemented as a singleton so we can call its load() and save()
methods from virtually anywhere inside our project. The settings will be loaded
from and saved to a preferences file defined in Constants.PREFERENCES .
Next, add the following code to the load() method of the same class:
public void load () {
sound = prefs.getBoolean("sound", true);
music = prefs.getBoolean("music", true);
volSound = MathUtils.clamp(prefs.getFloat("volSound", 0.5f),
0.0f, 1.0f);
 
Search WWH ::




Custom Search