【问题标题】:Top 5 scores from google leaderboard谷歌排行榜的前 5 名
【发布时间】:2014-07-26 07:06:27
【问题描述】:

我的要求是从排行榜中获得前 5 名并将其显示在我的应用中。

有一个方法 loadTopScores 但我猜它会在自己的 UI 中显示分数。

mGamesClint.loadTopScores(new OnLeaderboardScoresLoadedListener() {

            public void onLeaderboardScoresLoaded(int arg0, LeaderboardBuffer arg1,
                    LeaderboardScoreBuffer arg2) {
                // TODO Auto-generated method stub




            }
        }, LEADERBOARD_ID,LeaderboardVariant.TIME_SPAN_ALL_TIME  , LeaderboardVariant.COLLECTION_PUBLIC, 5, true);

那么有什么方法可以获取姓名和分数等个人数据吗?

姓名 1 : 最佳射手的姓名 1 score 1 : 得分最高者的得分 1

姓名 2:最佳射手的姓名 2 score 2 : 得分最高者的得分 2

……以此类推

我只想要名称字符串和分数整数,以便我可以在我的游戏中使用它。

请给我一些建议

【问题讨论】:

  • 您能在此处发布您的解决方案吗?我正在寻找同样的东西,但我还没有找到解决方案。
  • @JawadAmjad 检查我的答案。

标签: android google-api andengine google-play-games leaderboard


【解决方案1】:

在您的示例中,您走在正确的轨道上,您只需要在加载信息后提取信息。让它工作起来相当混乱,但我会告诉你我的this answer,它展示了如何为成就做这件事——为排行榜做这件事的工作方式相同,除了你将使用排行榜界面而不是那些使用的界面成就。

基本上,您将访问arg2 来获取数据,它应该看起来像这样:

mGamesClint.loadTopScores(new OnLeaderboardScoresLoadedListener() {

   public void onLeaderboardScoresLoaded(int arg0, LeaderboardBuffer arg1, LeaderboardScoreBuffer arg2) {

      // iterate through the list of returned scores for the leaderboard
      int size = arg2.getCount();
      for ( int i = 0; i < size; i++ )  {
         LeaderboardScore lbs = arg2.get( i );

         // access the leaderboard data
         int rank = i + 1;         // Rank/Position (#1..#2...#n)
         String name = lbs.getScoreHolderDisplayName();
         String scoreStr = lbs.getDisplayScore();
         long score = lbs.getRawScore();

         // now display or cache these values, or do whatever with them :)
      }

      arg2.close();
      arg1.close();
   }
}, LEADERBOARD_ID,LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 5, true);

我实际上并没有测试这个实现,但它应该可以工作(或者至少向您展示了足够多的东西,以便您可以修复我可能犯的任何错误)。

请记住,这将异步完成,因此方法的内容不会立即执行,而是在加载分数后执行。

【讨论】:

  • 有了这个,我可以从排行榜中获取分数和名称,并为排行榜创建我自己的 UI..
  • 很高兴,我很乐意提供帮助。由于没有任何例子,我可以在任何地方找到它,最好把它写下来以备将来参考。
  • 有机会发布工作代码吗?这似乎不起作用
  • @behelit 我无法为您编写代码,我只能为您指明正确的方向 - 这从来都不是工作代码,只是一个示例。如果您需要进一步解释,请查阅文档。
  • 我在任何地方都找不到 OnLeaderboardScoresLoadedListener 监听器,我已经搜索了文档和所有我能找到的东西,你能帮忙吗?
【解决方案2】:

这是我实现它的方法。

PendingResult<Leaderboards.LoadScoresResult> topScores = 
            Games.Leaderboards.loadTopScores(getApiClient(),LEADERBOARD_ID,LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 1, true);
            topScores.setResultCallback(new ResultCallback<Leaderboards.LoadScoresResult>() {

                @Override
                public void onResult(LoadScoresResult scoreResults) {

                    if(scoreResults != null) {
                        if (scoreResults.getStatus().getStatusCode() == GamesStatusCodes.STATUS_OK) {
                            scoreStringBuilder = new StringBuilder();
                            LeaderboardScoreBuffer scoreBuffer = scoreResults.getScores();
                            Iterator<LeaderboardScore> it = scoreBuffer.iterator();
                            while(it.hasNext()){
                                 LeaderboardScore temp = it.next();
                                 Log.d("PlayGames", "player"+temp.getScoreHolderDisplayName()+" id:"+temp.getRawScore() + " Rank: "+temp.getRank());
                                 scoreStringBuilder.append("|"+temp.getScoreHolderDisplayName()+"*"+temp.getRawScore());
                            }
                            UnityPlayer.UnitySendMessage(handlerName, "onGetScoreSucceededEventListener", "");
                             Log.d("PlayGames", "Call Sent for getHighScorewithPlayerNameSuccess ::: "+handlerName);


                        }
                    }

                }});

您可以根据需要使用它我创建了一个字符串并将其返回给 Unity,然后对其进行解析。 但此代码适用于最新的 Google Play 服务。

谢谢

【讨论】:

    【解决方案3】:

    我的解决方案如下,适用于最新的游戏服务。

      Games.Leaderboards.loadTopScores(mGamesClint,LEADERBOARD_ID, LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC, 5).setResultCallback(new ResultCallback<Leaderboards.LoadScoresResult>() {
    
            public void onResult(LoadScoresResult arg0) {
                // TODO Auto-generated method stub
    
                int size = arg0.getScores().getCount();
    
    
    
                for ( int i = 0; i < 3; i++ )  {
    
                    LeaderboardScore lbs = arg0.getScores().get(i);
    
                    String name = lbs.getScoreHolderDisplayName();
    
                    String score = lbs.getDisplayScore();
    
                    Uri urlimage = lbs.getScoreHolderHiResImageUri();
    
                     }
    
     }
    

    您可以从此处获取排行榜用户的所有数据,包括高分辨率图像,即。个人资料图片。 希望你在这里得到这个想法。

    【讨论】:

    • 看起来不错,但你应该在完成后关闭 scorebuffer(根据参考):arg0.getScores().close();
    【解决方案4】:

    此代码可让您获得排行榜上的前 5 名。

    private LeaderboardsClient mLeaderboardsClient; 
    
        mLeaderboardsClient.loadTopScores("HgkF9bOSF_UPAAILKE", LeaderboardVariant.TIME_SPAN_ALL_TIME, LeaderboardVariant.COLLECTION_PUBLIC,5,true).addOnSuccessListener(new OnSuccessListener<AnnotatedData<LeaderboardsClient.LeaderboardScores>>() {
                            @Override
                            public void onSuccess(AnnotatedData<LeaderboardsClient.LeaderboardScores> leaderboardScoresAnnotatedData) {
    
                                LeaderboardScoreBuffer scoreBuffer = leaderboardScoresAnnotatedData.get().getScores();
                                Iterator<LeaderboardScore> it = scoreBuffer.iterator();
                                while(it.hasNext()){
                                    LeaderboardScore temp = it.next();
                                    Log.d("PlayGames", "player"+temp.getScoreHolderDisplayName()+" id:"+temp.getRawScore() + " Rank: "+temp.getRank());
                                }
    
                            }
                        });
    

    【讨论】:

      猜你喜欢
      • 2018-06-26
      • 1970-01-01
      • 1970-01-01
      • 2015-11-19
      • 1970-01-01
      • 2017-02-24
      • 1970-01-01
      • 2018-01-31
      • 2017-10-19
      相关资源
      最近更新 更多