【问题标题】:How to save only new Highscore and put in Leaderboard如何只保存新的高分并放入排行榜
【发布时间】:2015-12-01 12:47:03
【问题描述】:

我们通常使用 sharedpreferences 来保存当前数据,例如

            SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE); 
            SharedPreferences.Editor editor = pref.edit();      
            editor.putInt("Score", gamescore);       
            editor.putInt("Level", gamelevel);  
            editor.commit();

然后使用它来获取保存在 sharedpreferences 中的数据

SharedPreferences pref = getSharedPreferences("SavedGame", MODE_PRIVATE);
gamelifes = pref.getInt("Score", 0);
gamelevel = pref.getInt("Level", 0);

但我们知道,每次获得新分数时,保存都会覆盖。

我怎样才能只保存一个新的高分?即如果用户获得的分数低于最新的高分,它将不会被保存,只有当它是一个新的高分时。另外,我怎样才能把这个分数放在谷歌排行榜上?

【问题讨论】:

  • 你可以得到最后一个高分并与新的比较。
  • 您应该始终在内存中的某处拥有high score。尝试在应用程序启动时读取高分并存储它。每次你要保存它时,与内存中的比较,如果它更多,则保存它,并将其分配给内存中的高分。应该很简单。
  • @Panther 所以这就像将当前分数与高分进行比较是更高还是更低?有一些示例代码吗?

标签: java android leaderboard


【解决方案1】:

我不完全理解代码,但我认为您希望使用 if 语句

highscore = pref.getInt('Highscore", 0);
if (gamescore > highscore){
    pref.edit("Highscore, gamescore);
}

类似的东西

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-08-02
    • 2019-10-22
    • 2016-10-21
    • 2014-11-13
    • 2018-02-07
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多