Game Development Reference
In-Depth Information
Finally, we have the same convenience method we used in Super Jumper to ease the pain
of playing back a sound effect. When the user disables sound, we don't play anything in
this method.
Note
Although this method of loading and managing assets is easy to implement, it can become
a mess if you have more than a handful of assets. Another issue is that sometimes not all assets
will fit into the memory all at once. For simple games, like the ones we've developed in this topic,
the method is fine. We often use it in our games, as well. For larger games, you have to consider a
more elaborate asset management strategy.
The Settings Class
As with the
Assets
class, for the
Settings
class, we can again reuse what we wrote for the
previous games, to some extent. We can now store an additional Boolean that tells us whether
the user wants to use the touchscreen or the accelerometer for moving the ship. We can drop
the high-score support, as we don't need to keep track of this. (As an exercise, you can, of
course, reintroduce both the high-scores screen and the saving of these scores to the SD card.)
Listing 12-2 shows the code.
Listing 12-2. Settings.java, Same Old, Same Old
package
com.badlogic.androidgames.androidinvaders;
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 static boolean
touchEnabled
=
true
;
public final static
String
file
= ".androidinvaders";
First, we store whether the sounds are enabled and whether the user wants to use touch input to
navigate the ship. The settings will be stored in the file
.androidinvaders
on the SD card.
public static void
load(FileIO files) {
BufferedReader in =
null
;
try
{
in =
new
BufferedReader(
new
InputStreamReader(files.readFile(
file
)));
soundEnabled
= Boolean.
parseBoolean
(in.readLine());
touchEnabled
= Boolean.
parseBoolean
(in.readLine());
}
catch
(IOException e) {
// :( It's ok we have defaults
}
catch
(NumberFormatException e) {