Settings Keeping Track of User Choices and High Scores

There are two other things that we need to load in the loading screen: the user settings and the high scores. If you look back at the main menu and high-scores screens in Chapter 3, you'll see that we allow the user to toggle the sounds, and that we store the top five high scores. We'll save these settings to the external storage so that we can reload them the next time the game starts. For this, we'll implement another simple class, called Settings, as shown in Listing 6-3.

Listing 6-3. Settings.java, Which Stores Our Settings and Loads/Saves Them package com.badlogic.androidgames.mrnom;

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 int[] highscores = new int[] { 100, 80, 50, 30, 10 };

Whether sound effects are played back is determined by a public static boolean called soundEnabled. The high scores are stored in a five-element integer array, sorted from highest to lowest. We define sensible defaults for both settings. We can access these two members the same way we access the members of the Assets class.

public static void load(FileIO files) { BufferedReader in = null; try {

in = new BufferedReader(new InputStreamReader(

files.readFile(".mrnom"))); soundEnabled = Boolean.parseBoolean(in.readLine()); for (int i = 0; i < 5; i++) {

highscores[i] = Integer.parse!nt(in.readLine());

// :( It's ok we have defaults } catch (NumberFormatException e) {

// :/ It's ok, defaults save our day } finally { try {

The static load() method tries to load the settings from a file called .mrnom from the external storage. It needs a FileIO instance for that, which we pass to the method. It assumes that the sound setting and each high-score entry is stored on a separate line and simple reads them in. If anything goes wrong (e.g., if the external storage is not available or there is no settings file yet), we simply fall back to our defaults and ignore the failure.

public static void save(FileIO files) { BufferedWriter out = null; try {

out = new BufferedWriter(new OutputStreamWriter(

files.writeFile(".mrnom"))); out.write(Boolean.toString(soundEnabled)); for (int i = 0; i < 5; i++) {

out.write(Integer.toString(highscores[i]));

Next up is a method called save(). It takes the current settings and serializes them to the .mrnom file on the external storage (e.g., /sdcard/.mrnom).The sound setting and each high-score entry is stored as a separate line in that file, as expected by the load() method. If something goes wrong, we just ignore the failure and use the default values defined earlier. In an AAA title, you might want to inform the user about this loading error.

public static void addScore(int score) { for (int i = 0; i < 5; i++) { if (highscores[i] < score) { for (int j = 4; j > i; j--)

highscores[j] = highscores[j - 1]; highscores[i] = score; break;

The final method, addScore(), is a convenience method. We will use it to add a new score to the high scores, automatically resorting them depending on the value we want to insert.

0 0

Post a comment