The Settings Class

Not a lot has changed. Listing 9-2 shows the code of our slightly modified Settings class.

Listing 9-2. Settings.java, Our Slightly Modified Settings Class, Stolen 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 = 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] = Integer.parse!nt(in.readLine());

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

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

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

out = new BufferedWriter(new OutputStreamWriter( files.writeFile(file)));

out.write(Boolean.toString(soundEnabled));

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

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-l]; highscores[i] = score; break;

The only difference from the Mr. Nom version of this class is the file we read and write the settings from and to. Instead of .mrnom we now use the file .superjumper.

0 0

Post a comment