Reputation: 81
I have a little game and I need to store some score information and some data if a stage is open or closed. I tried with externalstorage, internal storage and now with shared preferences. My game has only one activity and screens. A screen has a methond called pause which is called when we change the screen and also at the end when onPause is called too. I put the save method in every screen but no succes. My class GameImpl extends Activity. so I called the method load and save it a GameImpl instance which is the same in every class screen that I have. The code in class Settigs.java is below with the two methods load and save:
package com.spaceguardians;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
public class Settings {
public static int Open = 1;
public static int Locked = 0;
public static final int NR_STAGES = 8;
public static final float NOT_PLAYED = 0;
public static int planet1 = Open;
public static int planet2 = Locked;
public static int planet3 = Locked;
public static int planet4 = Locked;
public static int[] stagePlanet1 = { Open, Locked, Locked, Locked, Locked, Locked, Locked, Locked };
public static int[] stagePlanet2 = { Locked, Locked, Locked, Locked, Locked, Locked, Locked, Locked };
public static int[] stagePlanet3 = { Locked, Locked, Locked, Locked, Locked, Locked, Locked, Locked };
public static int[] stagePlanet4 = { Locked, Locked, Locked, Locked, Locked, Locked, Locked, Locked };
public static float[] stagePlanet1Score = { 00.00f, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED,
NOT_PLAYED, NOT_PLAYED, NOT_PLAYED };
public static float[] stagePlanet2Score = { NOT_PLAYED, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED,
NOT_PLAYED, NOT_PLAYED, NOT_PLAYED };
public static float[] stagePlanet3Score = { NOT_PLAYED, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED,
NOT_PLAYED, NOT_PLAYED, NOT_PLAYED };
public static float[] stagePlanet4Score = { NOT_PLAYED, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED, NOT_PLAYED,
NOT_PLAYED, NOT_PLAYED, NOT_PLAYED };
public static boolean soundEnabled = true;
public static final String spaceGuardiansPref = "spaceGuardiansPref" ;
public static void load(Context context) {// FileIO files) {
SharedPreferences mySharedPreferences = context.getSharedPreferences(spaceGuardiansPref,
Activity.MODE_PRIVATE);
soundEnabled = mySharedPreferences.getBoolean("sound", true);
planet1 = mySharedPreferences.getInt("planet1", Open);
planet2 = mySharedPreferences.getInt("planet2", Locked);
planet3 = mySharedPreferences.getInt("planet3", Locked);
planet4 = mySharedPreferences.getInt("planet4", Locked);
for (int i = 0; i < Settings.NR_STAGES; i++) {
if (i == 0) {
Settings.stagePlanet1[i] = mySharedPreferences.getInt("stagePlanet1" + i, Open);
Settings.stagePlanet1Score[i] = mySharedPreferences.getFloat("stagePlanet1Score", 0);
} else {
Settings.stagePlanet1[i] = mySharedPreferences.getInt("stagePlanet1" + i, Locked);
Settings.stagePlanet1Score[i] = mySharedPreferences.getFloat("stagePlanet1Score", NOT_PLAYED);
}
Settings.stagePlanet2[i] = mySharedPreferences.getInt("stagePlanet2" + i, Locked);
Settings.stagePlanet2Score[i] = mySharedPreferences.getFloat("stagePlanet2Score", NOT_PLAYED);
Settings.stagePlanet3[i] = mySharedPreferences.getInt("stagePlanet3" + i, Locked);
Settings.stagePlanet3Score[i] = mySharedPreferences.getFloat("stagePlanet3Score", NOT_PLAYED);
Settings.stagePlanet4[i] = mySharedPreferences.getInt("stagePlanet4" + i, Locked);
Settings.stagePlanet4Score[i] = mySharedPreferences.getFloat("stagePlanet4Score", NOT_PLAYED);
}
}
public static void save(Context context) {// FileIO files) {
SharedPreferences mySharedPreferences = context.getSharedPreferences(spaceGuardiansPref,
Activity.MODE_PRIVATE);
SharedPreferences.Editor editor = mySharedPreferences.edit();
editor.putBoolean("sound", soundEnabled);
editor.putInt("planet1", planet1);
editor.putInt("planet2", planet2);
editor.putInt("planet3", planet3);
editor.putInt("planet4", planet4);
for (int i = 0; i < Settings.NR_STAGES; i++) {
editor.putInt("stagePlanet1" + i, Settings.stagePlanet1[i]);
editor.putFloat("stagePlanet1Score" + i, Settings.stagePlanet1Score[i]);
editor.putInt("stagePlanet2" + i, Settings.stagePlanet2[i]);
editor.putFloat("stagePlanet2Score" + i, Settings.stagePlanet2Score[i]);
editor.putInt("stagePlanet3" + i, Settings.stagePlanet3[i]);
editor.putFloat("stagePlanet3Score" + i, Settings.stagePlanet3Score[i]);
editor.putInt("stagePlanet4" + i, Settings.stagePlanet4[i]);
editor.putFloat("stagePlanet4Score" +i, Settings.stagePlanet4Score[i]);
}
editor.commit();
}
For now I believe just the score is not displaying propretly. The sound is ok, It save the state and the stages/planets are oppend and closed correctly but the score is 00.00 for all the stages that are open. I do not know why..
Upvotes: 0
Views: 429
Reputation: 21421
The reason is that you are appending i
to your score preference name when saving it:
editor.putFloat("stagePlanet4Score" +i, Settings.stagePlanet4Score[i]);
^^^
But you are not adding it when loading the score:
Settings.stagePlanet4Score[i] =
mySharedPreferences.getFloat("stagePlanet4Score", NOT_PLAYED);
^^
Upvotes: 1