Andengine で Android 用のゲームを開発しています。レベルアンロックデータ、ハイスコアデータなどを保存するために 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));