我正在为 Android 开发 Andengine 的游戏。我决定使用 SharedPreferences 来保存关卡解锁数据、高分数据等。我实现了保存数据,但是当我再次打开我的游戏时,它正在将其数据重置为初始化数据。这就是为什么即使用户通过了一些关卡,所有关卡也会被锁定。
我使用这个类:
package com.example.aaa.extras;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
public class UserData {
private static UserData INSTANCE;
private static final String PREFS_NAME = "GAME_USERDATA";
/* These keys will tell the shared preferences editor which data we're trying to access */
private static final String UNLOCKED_LEVEL_KEY = "GAME_USERDATA";
private static final String SOUND_KEY = "soundKey";
//private static final String LIFE = "playerHealth";
private static final String HIGHSCORE = "GAME_USERDATA";
private static final String EDITED[] = {"GAME_USERDATA", "GAME_USERDATA", "GAME_USERDATA", "GAME_USERDATA", "GAME_USERDATA", "GAME_USERDATA"};
/* Create our shared preferences object and editor which will be used to save and load data */
private SharedPreferences mSettings;
private SharedPreferences.Editor mEditor;
//keep track of max unlocked level
public int mUnlockedLevel = 1; //public int mUnlockedLevel = 5;
//keep track of player high score
public int mHighScore = 0;
public boolean mEditedLevel[] = {false, false, false, false, false, false};
//keep track of player health
//public int health = 100;
//keep track of whether or not not sound is enabled
private boolean mSoundEnabled;
UserData() {
}
public synchronized static UserData getInstance() {
if (INSTANCE == null){
INSTANCE = new UserData();
Log.v("","Creates a new instance of UserData");
}
Log.v("","returns the instance of UserData");
return INSTANCE;
}
public synchronized void init(Context pContext){
if (mSettings == null){
mSettings = pContext.getSharedPreferences(PREFS_NAME, pContext.MODE_PRIVATE);
mEditor = mSettings.edit();
mUnlockedLevel = mSettings.getInt(UNLOCKED_LEVEL_KEY, 1);
mHighScore = mSettings.getInt(HIGHSCORE, 0);
mSoundEnabled = mSettings.getBoolean(SOUND_KEY, true);
for(int i=0;i<6;i++)
mEditedLevel[i] = mSettings.getBoolean(EDITED[i], false);
Log.v("","Set up initial values for UserData " + mUnlockedLevel + " " + mHighScore + " " + mSoundEnabled);
}
}
public synchronized int getMaxUnlockedLevel() {
return mUnlockedLevel;
}
public synchronized boolean isSoundMuted() {
return mSoundEnabled;
}
public synchronized int getHighScore() {
Log.v("","HighScore before increment " + mHighScore);
return mHighScore;
}
public synchronized boolean getEdited(int i) {
Log.v("","mEditedLevel before increment " + mEditedLevel[i]);
return mEditedLevel[i];
}
public synchronized void unlockNextLevel() {
mUnlockedLevel++;
mEditor.putInt(UNLOCKED_LEVEL_KEY, mUnlockedLevel);
mEditor.commit();
}
public synchronized void setSoundMuted(final boolean pEnableSound){
mSoundEnabled = pEnableSound;
mEditor.putBoolean(SOUND_KEY, mSoundEnabled);
mEditor.commit();
}
public synchronized void setHighScore(final int newHighScore){
mHighScore = newHighScore;
Log.v("","mHighScore is " + mHighScore);
mEditor.putInt(HIGHSCORE, mHighScore);
mEditor.commit();
}
public synchronized void setEdited(int i, final boolean newEdited){
mEditedLevel[i] = newEdited;
Log.v("","mEditedLevel is " + mEditedLevel[i]);
mEditor.putBoolean(EDITED[i], mEditedLevel[i]);
mEditor.commit();
}
}
我在 startActivity(first activity) 中创建并 init() 它,如下所示:
public void onCreateScene(OnCreateSceneCallback pOnCreateSceneCallback) throws IOException
{
SceneManager.getInstance().createSplashScene(pOnCreateSceneCallback);
UserData userData = UserData.getInstance();
userData.init(this);
}
我在任何需要像这样使用的类中使用它:
UserData.getInstance().setEdited(0, true);
UserData.getInstance().getEdited(0));