Search code examples
javaandroiddata-persistence

High score data persistence?


I am trying to save a high score for my game. I have a mainscreen activity, a gamescreen activity, and a redirectscreen activity (for when a player loses). Here is a method form the gamescreen activity that runs when a player dies:

public void goToRedirectScreen()
{
    //getting preferences
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", this.MODE_PRIVATE);
    int highScore = prefs.getInt("highScore", 0); //0 is the default value

    rectBlack1.setFixedColor(0xFFFF0015);
    rectBlack2.setFixedColor(0xFFFF0015);
    rectBlack3.setFixedColor(0xFFFF0015);
    rectWhite1.setFixedColor(0xFFFF0015);
    rectWhite2.setFixedColor(0xFFFF0015);
    rectWhite3.setFixedColor(0xFFFF0015);

    //Sound when player dies
    MediaPlayer mediaPlayer = MediaPlayer.create(this, R.raw.deathsound);
    mediaPlayer.start();

    Intent intent = new Intent(this, Redirect.class);

    if (mScoreboard.getScore() > highScore){
        mScoreboard.setHighScore(mScoreboard.getScore());
    }

    intent.putExtra("score", mScoreboard.getScore() + "");
    intent.putExtra("highScore", mScoreboard.getHighScore() + "");
    startActivity(intent);
}

And here is some of my code for the redirect screen that recieves the intent:

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

    Intent intent = getIntent();
    mScore = intent.getStringExtra("score");
    mHighScore = intent.getStringExtra("highScore");

    //**********setting preferences************
    SharedPreferences prefs = this.getSharedPreferences("myPrefsKey", this.MODE_PRIVATE);
    SharedPreferences.Editor editor = prefs.edit();
    editor.putInt("highScore", Integer.parseInt(mHighScore));
    editor.commit();

    ButterKnife.inject(this);

    scoreTextView.setText(mScore);
    highScoreTextView.setText(mHighScore);


    playAgainButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            playAgain();

        }
    });

    mainMenuButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            goToMainMenu();
        }
    });



}

To do this I referenced Need to save a high score for an Android game to do this, but now I don't what I did wrong because the highscore and score textview are always the same no matter what score I get when I test the game even if it is higher than my previous score.

Here is my Scoreboard class in case it is useful:

public class Scoreboard {
private int mScore = 0;
private int mHighScore = 0;

public void addPoint() {
    mScore += 1;
}

public int getHighScore() {
    return mHighScore;
}

public void setHighScore(int highScore) {
    mHighScore = highScore;
}

public int getScore() {
    return mScore;
}

public void setScore(int score) {
    mScore = score;
}

}

Solution

  • Set the loaded value as the high score after you load it:

    int highScore = prefs.getInt("highScore", 0); //0 is the default value
    mScoreboard.setHighScore(highScore);
    

    Otherwise, in the case where the new score isn't a new high score, you're never actually using the loaded value and so the existing high score won't be passed to your redirect screen.