Вопрос

I'm working on a multi level Android game and I want to pull the highscores after sign in. I'm doing this:

Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(LEADER_BOARDS[i]),
                LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
                .setResultCallback(this);

But the callback doesn't give me the leaderboard id:

@Override
public void onResult(LoadPlayerScoreResult res) {

}

I only get the score (res.getScore()). So do I have to create callbacks for each of my 25 levels or is there a better solution?

This doesn't work as well:

for (int i = 0; i < 25; i++)
    Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(LEADER_BOARDS[i]),
                LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
                .setResultCallback(new ResultCallback<LoadPlayerScoreResult>() {

                    @Override
                    public void onResult(LoadPlayerScoreResult res) {
                        // can't use variable i here
                    }
    });
Это было полезно?

Решение

Define this inner class in your java file:

public class Leaderboard implements ResultCallback<Leaderboards.LoadPlayerScoreResult> {

        private Leaderboard(String id) {
            leaderboardId = id;
        }

        private String leaderboardId = "";

        @Override
        public void onResult(LoadPlayerScoreResult arg0) {
            // TODO Auto-generated method stub
            System.out.println(leaderboardId);
        }

}

Now use this code:

for (int i = 0; i < 25; i++) { 
    Games.Leaderboards.loadCurrentPlayerLeaderboardScore(getApiClient(), getString(LEADER_BOARDS[i]),
    LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC)
    .setResultCallback(new Leaderboard(getString(LEADER_BOARDS[i])));
}

Now let me explain how this works a little. The Leaderboard class implements ResultCallback so it's used in the setResultCallback method after you get the score for the player. Now, in the for loop, each call to get a score is having a different object of class leaderboard being set as the result callback, and each object is being given the unique leaderboard id which is being saved in the class using the constructor, and as you can see in the class, there is a method that will give you the leaderboardid back in the onresult.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top