Saving high scores in Android game - Shared Preferences

為{幸葍}努か 提交于 2019-12-20 07:14:59

问题


Recently I am developing a simple android game. For the scoring part, I have on many websites that shared preferences are best to save the high score. Now, what if I need to save high scores of different levels in my game? I wish to save top three scorers score for each level...


回答1:


To save your scores you can do something like this:

// prepare the data: put the String values of the scores of the first 3 users
// in one String array for each level
String[] firstLevelHighscores = new String[] { 
    firstUserLevel1Score, secondUserLevel1Score, thirdUserLevel1Score 
};
String[] secondLevelHighscores = new String[] { 
    firstUserLevel2Score, secondUserLevel2Score, thirdUserLevel2Score 
};
String[] thirdLevelHighscores = new String[] { 
    firstUserLevel3Score, secondUserLevel3Score, thirdUserLevel3Score 
};

// now save them in SharedPreferences
SharedPreferences sharedPref = getSharedPreferences("LevelScores", 
        Context.MODE_PRIVATE);
Editor editor = sharedPref.edit();
editor.putStringSet("level1", firstLevelHighscores);
editor.putStringSet("level2", secondLevelHighscores);
editor.putStringSet("level3", thirdLevelHighscores);

Note that you can put even more user's scores into the String array. And if you need to save scores for more levels, you simply create more arrays.

To retrieve the saved data from SharedPreferences, you do it like this:

SharedPreferences sharedPref = getSharedPreferences("LevelScores", 
        Context.MODE_PRIVATE);
String[] firstLevelHighscores = sharedPref.getStringSet("level1", null);
String[] secondLevelHighscores = sharedPref.getStringSet("level2", null);
String[] thirdLevelHighscores = sharedPref.getStringSet("level3", null);

I assume you're able to convert int to String and vice versa. Hope it works for you this way.




回答2:


A lot of ways to save your scores to the SharedPreference. All depending on your style of implementation.

You can simply use the putStringSet(key, String[]) where as the key will be the level and the String[] the 1ste, 2nd and 3th place.




回答3:


score setting

static final String[] LEVEL = {"level1","level2","level3"};

int bestScore1 = 100;
int bestScore2 = 90;
int bestScore3 = 80;

SharedPreferences sp = getSharedPreferences(LEVEL[0],Activity.MODE_PRIVATE);
    SharedPreferences.Editor editor = sp.edit();

    editor.putInt("First", bestScore1);
    editor.putInt("Second", bestScore2);
    editor.putInt("Third", bestScore3);

    editor.commit();

but you need to count LEVEL's index

score getting

SharedPreferences sp = getSharedPreferences(LEVEL[0], Activity.MODE_PRIVATE);

    bestScore1 = sp.getInt("First", 0);
    bestScore2 = sp.getInt("Second", 0);
    bestScore3 = sp.getInt("Third", 0);

hmm.. but I think it is not best way for your question :<



来源:https://stackoverflow.com/questions/15898490/saving-high-scores-in-android-game-shared-preferences

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!