1

そのため、sharedPreferencesを使用して最高のスコアを保存しようとしていますが、問題が発生しています。最高のスコアだけを取得して表示するように設定する方法がわかりません。

私が今持っているものは、プレイヤーが受け取った現在のスコアのみを表示します。これが私がこれまでに持っているもので機能しないものです:

public class GameOptions extends Activity {
int theScore;
TextView highScore;
public static String filename = "MyHighScore";
SharedPreferences spHighScore;
int dataReturned;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.in_game_menu);
    TextView tvTheScore = (TextView) findViewById(R.id.textView2);
    TextView highScore = (TextView) findViewById(R.id.high_score);
    Bundle gotScore = getIntent().getExtras();
    theScore = gotScore.getInt("scoreKey"); 
    //String thisIsTheScoreToDisplay = theScore.toString();
    tvTheScore.setText("SCORE: "+theScore);

    spHighScore = getSharedPreferences(filename, 0);
    SharedPreferences.Editor editor = spHighScore.edit();

    editor.putInt("highScoreKey", theScore);
    editor.commit();

    int dataReturned = spHighScore.getInt("highScoreKey", 0);
    highScore.setText("" + dataReturned);
4

2 に答える 2

3

これにより、スコアを保存できます

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    SharedPreferences.Editor prefsEditor = myPrefs.edit();
    prefsEditor.putString(MY_SCORE_KEY, HIGHEST_SCORE);
    prefsEditor.commit();

このようになります

SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
    String highestScore = myPrefs.getString(MY_SCORE_KEY, "nothing");

これがお役に立てば幸いです。

于 2012-03-17T04:14:21.353 に答える
2

ハイスコ​​アを保存する直前に、設定に保存されている現在のハイスコアを取得し、ハイスコアよりも小さいかどうかを確認します。それより少ない場合は、新しいものを共有設定に保存するか、過去のものを最高のままにします。

于 2012-03-17T04:17:06.357 に答える