我们通常使用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);
但正如我们所知,每次做出新分数时,保存都会覆盖。
如何只保存一个新的高分?即,如果用户获得的分数低于最新的高分,则不会被保存,只有当它是新的高分时。另外,我怎样才能将这个分数放在 Google 排行榜中?
最佳答案
我不完全理解代码,但我认为您希望使用 if 语句
highscore = pref.getInt('Highscore", 0);
if (gamescore > highscore){
pref.edit("Highscore, gamescore);
}
类似的东西
关于java - 如何只保存新的高分并放入排行榜,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32413650/