I am making a mario type game and i want to save my highscore but when i restart my app highscore becomes zero.I am using libGdx and i don't know how to use shared preferences and where to write in create or render method. I am using this code but it does not work.
...protected Preferences highScore(){
if (score> highScore){
prefs.putInteger("highScore",score);
this.highScore=prefs.getInteger("highScore",0);
prefs.flush():
}
return prefs;
}...
Hard to tell without more code... I assume that when you start your game you initialize your preference object and read the current highscore? otherwise highscore would be defaulted to 0;
Then in your game, whenever you update your score:
// When you start the game. Execute once at the beginning of each game.
Preferences prefs = Gdx.app.getPreferences("My Preferences");
int this.highScore=prefs.getInteger("highScore",0);
// When you start finish the game. Execute when the player finished the game
if (score> highScore){
prefs.putInteger("highScore",score);
this.highScore=prefs.getInteger("highScore",0);
prefs.flush():
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.