【问题标题】:Update (increment/decrement) leaderboard score in Google Play Games在 Google Play 游戏中更新(递增/递减)排行榜得分
【发布时间】:2017-09-12 01:41:36
【问题描述】:
我使用以下代码将用户的分数提交给 Google Play Games:
if(getApiClient().isConnected()){
Games.Leaderboards.submitScore(getApiClient(), getString(R.string.number_guesses_leaderboard),newScore);
}
但这不会增加排行榜的分数,它只是替换分数。始终显示相同的值。 newScore 是我想要增加当前分数的数量。
【问题讨论】:
标签:
android
android-studio
google-play-services
google-play-games
【解决方案1】:
例如,您需要使用SharedPreferences 在本地保存当前分数,然后将新的total 分数提交到 Google Play 游戏服务。看看 Google Play 游戏团队的this 示例,特别是他们使用AccomplishmentsOutbox 在本地存储分数,直到将分数传输到 API。
【解决方案2】:
这是我用来增加 google play 服务排行榜的方法。如果返回的分数为空,则发布分数 1。否则,当前分数会增加 1。例如,这对于跟踪每日分数很有用。请注意使用 TIME_SPAN_DAILY 来指示所需的时间跨度。这将检索当天发布的最高分数,假设在设置排行榜时指示“越高越好”。
private void incrementDailyLeaderboard(final LeaderboardsClient leaderboardsClient, final String leaderboardId) {
leaderboardsClient.loadCurrentPlayerLeaderboardScore(
leaderboardId,
LeaderboardVariant.TIME_SPAN_DAILY,
LeaderboardVariant.COLLECTION_PUBLIC
).addOnSuccessListener(this, new OnSuccessListener<AnnotatedData<LeaderboardScore>>(){
@Override
public void onSuccess(AnnotatedData<LeaderboardScore> leaderboardScoreAnnotatedData) {
//Log.v("onSuccess","onSuccess");
if (leaderboardScoreAnnotatedData != null) {
long score = 0;
if (leaderboardScoreAnnotatedData.get() != null) {
score = leaderboardScoreAnnotatedData.get().getRawScore();
//Log.v("Your score is", Long.toString(score));
}
leaderboardsClient.submitScore(leaderboardId, ++score);
}
}
});
}