我需要从Game Center检索经过身份验证的玩家提交的分数。我使用此代码来获得分数,但它只获得最高分(排行榜的最高分不是指定玩家的分数)。如何检索经过身份验证的玩家的分数?
- (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);
}
}];
}
}
答案 0 :(得分:10)
您可以使用以下代码:
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];
}
}];
}
答案 1 :(得分:6)
您只需针对给定的loadScoresWithCompletionHandler
点击GKLeaderboard
,然后将自动board.localPlayerScore
填写该主板。
例如,
- (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 ) ;
}] ;
}
}] ;
}
答案 2 :(得分:5)
您也可以尝试使用一系列玩家ID来启动排行榜,以缩小玩家数量:
GKLeaderboard *board = [[[GKLeaderboard alloc] initWithPlayerIDs:[NSArray arrayWithObject:myGCPlayerID]] autorelease];
答案 3 :(得分:1)
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
}
})
}