libGdx 中的共享首选项以获得高分



我正在制作一个马里奥类型的游戏,我想保存我的高分,但当我重新启动我的应用程序时,高分变为零。我正在使用libGdx,我不知道如何使用共享首选项以及在创建或渲染方法中编写的位置。 我正在使用此代码,但它不起作用。

...protected Preferences highScore(){
if (score> highScore){
prefs.putInteger("highScore",score);
this.highScore=prefs.getInteger("highScore",0);
prefs.flush():
}
return prefs;
}...

没有更多代码很难分辨... 我假设当您启动游戏时,您会初始化您的首选项并读取当前高分?否则,高分将默认为 0;

  1. 初始化首选项。 例如,您可以在创建游戏时执行此操作。

然后在您的游戏中,每当您更新分数时:

// 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():
}

最新更新