Game Development Reference
In-Depth Information
volMusic = MathUtils.clamp(prefs.getFloat("volMusic", 0.5f),
0.0f, 1.0f);
charSkin = MathUtils.clamp(prefs.getInteger("charSkin", 0),
0, 2);
showFpsCounter = prefs.getBoolean("showFpsCounter", false);
}
The
load()
method will always try its best to find a suitable and, more importantly,
valid value. This is achieved by supplying default values to the getter methods of the
Preferences
class. For example, the call
getFloat("volSound", 0.5f)
will return
a value of
0.5f
if there is no value found for the key named
volSound
. Before the
value of the sound volume is finally stored, it is also passed to the
clamp()
utility
method to ensure that the value is within the allowed range of values, which is
0.0f
and
1.0f
here.
Next, add the following code to the
save()
method of the same class:
public void save () {
prefs.putBoolean("sound", sound);
prefs.putBoolean("music", music);
prefs.putFloat("volSound", volSound);
prefs.putFloat("volMusic", volMusic);
prefs.putInteger("charSkin", charSkin);
prefs.putBoolean("showFpsCounter", showFpsCounter);
prefs.flush();
}
The
save()
method is pretty straightforward as it just takes the current values of its
public variables and puts them into the map of the preferences file. Finally,
flush()
is called on the preferences file to actually write the changed values into the file.
The second action is to create another class that abstracts all selectable character
skins. Create a new file called
CharacterSkin
and add the following lines of code:
package com.packtpub.libgdx.canyonbunny.util;
import com.badlogic.gdx.graphics.Color;
public enum CharacterSkin {
WHITE("White", 1.0f, 1.0f, 1.0f),
GRAY("Gray", 0.7f, 0.7f, 0.7f),
BROWN("Brown", 0.7f, 0.5f, 0.3f);
private String name;
private Color color = new Color();