2012-02-04 34 views
2

我即將完成我的第一個應用程序(並需要)使用Game Center。我沒有取得成就,只是一個高分系統。我已經插入了所有東西,我的應用程序已在啓用了Game Center的iTunesConnect中註冊,並且當我記錄高分時,它會顯示在列表中。GKScore的排名總是爲0

不工作的一件事是「等級」。在完成塊[GKScore reportScoreWithCompletionHandler:]中,即使用戶獲得了新的高分,我的GKScore對象的rank屬性也始終爲0。

例如,在我的應用程序,當我運行:

GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"1"]; 
scoreReporter.value = 2200003; // test value 
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) { 

    if (error != nil) 
    { 
     NSLog(@"An error occured reporting the Game Center score: %@", error); 
    } 

    NSLog(@"Score: %@", scoreReporter); 
    NSLog(@"Score: %d", scoreReporter.rank); 
}]; 

...沒有出現錯誤,輸出爲:

Score: <GKScore: 0x361a3c0><0x361a3c0> player=G:1127411264 rank=0 date=2012-02-04 22:19:52 +0000 value=2200002 formattedValue=(null) context=(null) 
Score: 0 

有可能我丟失的東西在iTunesConnect?一旦我離開沙盒,排名會開始工作嗎?任何指針在正確的方向將不勝感激。

回答

3

你的代碼沒有錯誤。如果您只是創建GKScore對象,rank的值始終爲0。它只對從Game Center收到的分數對象有效。 請仔細閱讀本文檔: https://developer.apple.com/library/ios/#documentation/GameKit/Reference/GKScore_Ref/Reference/Reference.html

+0

這是正確的。當然,我曾閱讀過這些文檔,但出於某種原因誤解了「此屬性的值在新初始化的GKScore對象上未定義,僅對從Game Center收到的得分對象有效。」意味着等級將填補完成塊。這當然意味着,如果你創建了GKScore,排名將始終爲0.問題仍然存在:「你如何獲得剛剛發佈的分數排名?」我可能會提出另一個問題提出這個具體問題。 – laxcat 2012-02-19 15:49:17

0

laxcat,你不能得到一個等級的只是相加的分數立刻,但一段時間後,就可以執行請求來獲取用戶等級:

GKLeaderboard *lb = [[[GKLeaderboard alloc] init] autorelease]; 
lb.category = @"YOURLeaderBoardID"; 
lb. timeScope = GKLeaderboardTimeScopeToday; 
[lb loadScoresWithCompletionHandler:^(NSArray *scores, NSError *error) 
           { 
            GKLocalPlayer *lp = [GKLocalPlayer localPlayer]; 
            for (GKScore* score in scores) 
            { 
             if ([score.playerID sEqualToString:lp.playerID]) 
             { 
              NSLog(@"rank = %d", score.rank); 
             } 
            } 
           }]; 

請試試這個,並讓我知道它是否適合你。

1

您需要將您的整數轉換爲int64_t。在Objective-C中,這是一個LongLong。你可以改變這一點:

GKScore *myScore = [[GKScore alloc]initWithCategory:@"1"]; 
myScore.value = [[NSNumber numberWithInt:score] longLongValue]; //score should be of type int 
0

OBJ-C採用圓弧啓用 - 讓等級供玩家在排行榜 有過得分無需環路,乾脆直接訪問通過[YourAllocatedLeaderBoard] .localPlayerScore.rank(以完成處理程序)

GKLeaderboard *leaderBoard = [[GKLeaderboard alloc]init]; 
    [leaderBoard setIdentifier:@"yourLeaderboardID"]; 
    [leaderBoard setTimeScope:GKLeaderboardTimeScopeAllTime]; 

    [leaderBoard loadScoresWithCompletionHandler:^(NSArray<GKScore *> * _Nullable scores, NSError * _Nullable error) { 
     if (!error) { 
      NSLog(@"RANK %d",(int)leaderBoard.localPlayerScore.rank); 

     }else{ 
      NSLog(@"*error* %@",error.localizedDescription); 

     } 
    }]; 

    leaderBoard = nil;