简体   繁体   English

如何获得认证玩家的高分表格排行榜(游戏中心)

[英]How to get authenticated player's high score form leaderboard ( Game Center )

I need to retrieve authenticated player's submited score from Game Center.我需要从 Game Center 检索经过身份验证的玩家提交的分数。 I use this code to get the score, but it just gets the top score (best score of the leaderboard not the specified player's score).我使用这段代码来获得分数,但它只是获得最高分(排行榜的最佳分数而不是指定玩家的分数)。 How can I retrieve the authenticated player's score?如何检索经过身份验证的玩家的分数?

- (void) retrievePlayersScore {
    GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init]; 
    if (leaderboardRequest != nil) {
        leaderboardRequest.playerScope = GKLeaderboardPlayerScopeGlobal; 
        leaderboardRequest.timeScope = GKLeaderboardTimeScopeAllTime; 
        leaderboardRequest.range = NSMakeRange(1,1);
        [leaderboardRequest loadScoresWithCompletionHandler: ^(NSArray *scores, NSError *error) {
            if (error != nil) {
                // handle the error. if (scores != nil)
            }
            if (scores != nil){
                // process the score information.
                CCLOG(@"My Score: %d", ((GKScore*)[scores objectAtIndex:0]).value);
            } 
        }];
    }
}

You can use the following code:您可以使用以下代码:

GKLeaderboard *leaderboardRequest = [[GKLeaderboard alloc] init];

leaderboardRequest.identifier = _leaderboardIdentifier;

if (leaderboardRequest != nil) {
    [leaderboardRequest loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error){
        if (error != nil) {
            //Handle error
        }
        else{
            [delegate onLocalPlayerScoreReceived:leaderboardRequest.localPlayerScore];
        }
    }];
}

You just have to hit loadScoresWithCompletionHandler for a given GKLeaderboard , then automatically board.localPlayerScore will be filled out for that board.您只需为给定的GKLeaderboard loadScoresWithCompletionHandler然后会自动为该板填写board.localPlayerScore

So for example,例如,

- (void) getLoadLeaderboardPositions
{
  [GKLeaderboard loadLeaderboardsWithCompletionHandler:^(NSArray *leaderboards, NSError *nsError) {
    if( nsError != nil )
    {
      error( nsError, "get leaderboard score" ) ;
      return ;
    }

    for( GKLeaderboard* board in leaderboards )
    {
      // fetch score for minimum amt of data, b/c must call `loadScore..` to get MY score.
      board.playerScope = GKLeaderboardPlayerScopeFriendsOnly ;
      board.timeScope = GKLeaderboardTimeScopeAllTime ;

      NSRange range = {.location = 1, .length = 1};
      board.range = range ;

      [board loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) {
        printf( "YOUR SCORE ON BOARD %s WAS %lld\n", [board.title UTF8String], board.localPlayerScore.value ) ;
      }] ;
    }
  }] ;
}

You may also try to initiate the leader board by using an array of player id(s) in order to narrow the number of players:您也可以尝试使用一组玩家 ID 来启动排行榜,以缩小玩家数量:

GKLeaderboard *board = [[[GKLeaderboard alloc] initWithPlayerIDs:[NSArray arrayWithObject:myGCPlayerID]] autorelease];

Updated version using Swift使用 Swift 的更新版本

let localPlayer = GKLocalPlayer.localPlayer()

if localPlayer.isAuthenticated {
    let leaderboard = GKLeaderboard(players: [localPlayer])
    leaderboard.identifier = LEADERBOARD_ID
    leaderboard.timeScope = .allTime
    leaderboard.loadScores(completionHandler: {
        (scores, error) in

        let bestScore = scores?.first?.value

        if bestScore != nil {
            // Do something with bestScore
        }
    })
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM