Game Development Reference
In-Depth Information
Next, we have a mysterious method called
reload()
. Remember that the OpenGL ES context
will get lost when our application is paused. We have to reload the textures when the application
is resumed, and that's exactly what this method does. We also resume the music playback in
case sound is enabled.
public static void
playSound(Sound sound) {
if
(Settings.
soundEnabled
)
sound.play(1);
}
}
The final method of this class,
playSound()
, is a helper method we'll use in the rest of the
code to play back audio. Instead of having to check whether sound is enabled everywhere,
we encapsulate that check in this method.
Let's have a look at the modified
Settings
class.
The Settings Class
Not a lot has changed in the
Settings
class. Listing 9-2 shows the code of our slightly modified
Settings
class.
Listing 9-2. Settings.java, Our Slightly Modified Settings Class, Borrowed from Mr. Nom
package
com.badlogic.androidgames.jumper;
import
java.io.BufferedReader;
import
java.io.BufferedWriter;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.io.OutputStreamWriter;
import
com.badlogic.androidgames.framework.FileIO;
public class
Settings {
public static boolean
soundEnabled
=
true
;
public final static int
[]
highscores[i]
=
new int
[] { 100, 80, 50, 30, 10 };
public final static
String
file
= ".superjumper";
public static void
load(FileIO files) {
BufferedReader in =
null
;
try
{
in =
new
BufferedReader(
new
InputStreamReader(files.readFile(
file
)));
soundEnabled
= Boolean.
parseBoolean
(in.readLine());
for
(
int
i = 0; i < 5; i++) {
highscores[i]
[i] = Integer.
parseInt
(in.readLine());
}
}
catch
(IOException e) {
// :( It's ok we have defaults
}
catch
(NumberFormatException e) {