は、私は、単純なint型、スコアを保存するには、この質問Need to save a high score for an Android gameからこの答えを使用してみました保存されていないが、私は問題を抱えています。私のゲームのアクティビティから、newScoreというintを渡し、失われた画面のアクティビティに入れました。私はnewScore> highScoreかどうかをチェックし、もしそうならnewScoreを新しい高得点として入れます。私は両方とも表示します。現時点では常に、画面上で常に高いスコアとスコアを表示します。だから私が10点を獲得しても、次回は戻って1点を獲得すれば1点がハイスコアとして表示されます。ハイスコアは
それは私がまた私からの私の終了方法に出してあげる `
public class LoseScreen extends AppCompatActivity`:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.lose_screen);
Bundle b = getIntent().getExtras();
int newScore = b.getInt("newScore");
TextView textView6 = (TextView) findViewById(R.id.textView6);
textView6.setText("Score: " + newScore);
SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
int highScore = prefs.getInt("highScore", 0); //0 is the default value
if (newScore > highScore) {
prefs = this.getSharedPreferences("myPrefsKey", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = prefs.edit();
editor.putInt("highScore", newScore);
editor.commit();
highScore = newScore;
}
TextView textView8 = (TextView) findViewById(R.id.textView8);
textView8.setText("High Score: " + highScore);
}
から
大きい場合、これは私がしようとしたコードであり、高いスコアにアクセスし、新しいスコアに入れます主なゲーム活動;クラスが大規模で無関係だと思うので、クラス全体のコードを投稿しているわけではありません。私はそれを投稿して嬉しいですが、それが助けになるなら。
public void quit() {
Intent intent = new Intent(ColorMatch2.this,
LoseScreen.class);
int newScore = score;
Bundle b = new Bundle();
b.putInt("newScore", newScore); // Your score
intent.putExtras(b); // Put your score to your next
startActivity(intent);
}
スコアを保存しようとしましたか?問題はどこだ? (あなたのアプリにデータを保存するには、基本的に3つの方法があります:a)sharedPreferences b)ファイルc)databse) (sharedPreferencesを見てください!) –
私はsharedPreferencesで試してみました。最初のブロックは画面を失います。これはこの部分です:SharedPreferences prefs = this.getSharedPreferences( "myPrefsKey"、Context.MODE_PRIVATE); int highScore = prefs.getInt( "highScore"、0); // 0(newScore>ハイスコア){ 環境設定の=のthis.getSharedPreferences( "myPrefsKey"、Context.MODE_PRIVATE)場合、デフォルト値 あります。 SharedPreferences.Editor editor = prefs.edit(); editor.putInt( "highScore"、newScore); editor.commit(); highScore = newScore; } – Johnny
それはここのコメントで読みにくいですが、それは共有環境設定環境設定から始めて、最初のブロックでは約20行下だ= – Johnny