簡體   English   中英

Android保存高分

[英]Android Save High Scores

大家好,我正在為Android開發游戲,但有一個我在互聯網上找不到的問題。我想使用共享的首選項保存高分,這是代碼:

Play Class : 
        SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
        Editor edit = prefs.edit();
        edit.putInt("key", score);
        edit.commit();
        Toast.makeText(getApplicationContext(), "SAVED", Toast.LENGTH_LONG).show();

        Intent it = new Intent(getApplicationContext(),HighScore.class);
        startActivity(it);

這是高分列表代碼:

highscore = (TextView) findViewById(R.id.highscore_int);
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", 
                                                    Context.MODE_PRIVATE);
int score = prefs.getInt("key", 0); //0 is the default value
highscore.setText(""+score);

這可以正常工作,但可以保存所有分數,即使比以前小。 我只想保存比以前更大的分數。 我怎樣才能做到這一點? PS:對不起,我的英語,我不知道該如何突出顯示代碼:(

SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int oldScore = prefs.getInt("key", 0);  
if(newScore > oldScore ){
   Editor edit = prefs.edit();
   edit.putInt("key", newScore);
   edit.commit();
}

查看。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM