2011-12-06 110 views
15

這很簡單,我需要做的就是爲遊戲保存高分(整數)。我假設最簡單的方法是將它存儲在文本文件中,但我真的不知道如何去做這件事。需要爲Android遊戲保存高分

回答

39

如果你需要的是存儲整數然後SharedPreferences將是最適合你的使用方法:

//setting preferences 
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
Editor editor = prefs.edit(); 
editor.putInt("key", score); 
editor.commit(); 

爲了得到一個偏好:

//getting preferences 
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE); 
int score = prefs.getInt("key", 0); //0 is the default value 

當然與鍵取代"key"你的高分值和"myPrefsKey"與你的偏好的關鍵(這些可以是任何東西,只是把它們設置爲可識別和獨特的東西)。

+1

只是爲了確認,這會存儲數據,所以下次我運行遊戲時我可以獲得它嗎? – Zizo47

+2

是的,這些數據在您的應用運行之間依然存在。只要確保調用commit();在編輯器上爲他們保存! – dymmeh

+0

非常感謝,我現在試試 – Zizo47

2

使用shared preferences

public class Calc extends Activity { 
    public static final String PREFS_NAME = "MyPrefsFile"; 

    @Override 
    protected void onCreate(Bundle state){ 
     super.onCreate(state); 
     . . . 

     // Restore preferences 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
     boolean silent = settings.getBoolean("silentMode", false); 
     setSilent(silent); 
    } 

    @Override 
    protected void onStop(){ 
     super.onStop(); 

     // We need an Editor object to make preference changes. 
     // All objects are from android.context.Context 
     SharedPreferences settings = getSharedPreferences(PREFS_NAME, MODE_PRIVATE); 
     SharedPreferences.Editor editor = settings.edit(); 
     editor.putBoolean("silentMode", mSilentMode); 

     // Commit the edits! 
     editor.commit(); 
    } 
} 

它來存儲這樣的事情最簡單的方法。

2

我認爲this link將幫助你在它:

The SharedPreferences class provides a general framework that allows you to save and 
retrieve persistent key-value pairs of primitive data types. You can use 
SharedPreferences to save any primitive data: booleans, floats, ints, longs, and strings. 
This data will persist across user sessions (even if your application is killed). 

User Preferences 

Shared preferences are not strictly for saving "user preferences," such as what ringtone 
a user has chosen. If you're interested in creating user preferences for your 
application, see PreferenceActivity, which provides an Activity framework for you to 
create user preferences, which will be automatically persisted (using shared preferences). 

To get a SharedPreferences object for your application, use one of two methods: 

    getSharedPreferences() - Use this if you need multiple preferences files identified 
by name, which you specify with the first parameter. 
    getPreferences() - Use this if you need only one preferences file for your Activity. 
Because this will be the only preferences file for your Activity, you don't supply a name. 

To write values: 

    Call edit() to get a SharedPreferences.Editor. 
    Add values with methods such as putBoolean() and putString(). 
    Commit the new values with commit() 

To read values, use SharedPreferences methods such as getBoolean() and getString(). 

依我之見,爲您節省高分的最佳途徑是SharedPreferences。

0
public class HighScores extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_high); 

     //get text view 
     TextView scoreView = (TextView)findViewById(R.id.high_scores_list); 
     //get shared prefs 
     SharedPreferences scorePrefs = getSharedPreferences(PlayGame.GAME_PREFS, 0); 
     //get scores 
     String[] savedScores = scorePrefs.getString("highScores", "").split("\\|"); 
     //build string 
     StringBuilder scoreBuild = new StringBuilder(""); 
     for(String score : savedScores){ 
      scoreBuild.append(score+"\n"); 
     } 
     //display scores 
     scoreView.setText(scoreBuild.toString()); 
    } 

} 
+2

需要添加幾行文字來解釋此代碼塊的功能。 – Sufian