2016-02-19 72 views
1

我已經在互聯網上找到了答案。我在iTunes Connect中有一個排行榜設置,它顯示在我的遊戲中,但高分從未報告給排行榜。如何獲得高分提交/報告給Game Center排行榜?

這裏是我的GameViewController.m

 - (void)authenticateLocalPlayer { 
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer]; 

localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error) { 
    if (viewController != nil) [[UIApplication sharedApplication].delegate.window.rootViewController presentViewController:viewController animated:YES completion:nil]; 
    else { 
     if ([GKLocalPlayer localPlayer].authenticated) { 
      gameCenterEnabled = YES; 

      [[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) { 
       if (error != nil) NSLog(@"%@", [error localizedDescription]); 
       else _leaderboardIdentifier = leaderboardIdentifier; 
      }]; 
     } 
     else gameCenterEnabled = NO; 
    } 
}; 
    } 

    - (void)showLeaderboard { 
GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init]; 
gcViewController.gameCenterDelegate = self; 
gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards; 
gcViewController.leaderboardIdentifier = _leaderboardIdentifier; 
[self presentViewController:gcViewController animated:YES completion:nil]; 
    } 

- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController { 
[gameCenterViewController dismissViewControllerAnimated:YES completion:nil]; 
    } 

,並在我的MenuScene.m和EndScene.m我有這樣的代碼,以顯示其在屏幕上顯示的最好成績的代碼我對排行榜標籤。

 _labelScoreBest = [[SimpleLabel alloc] initWithText:[NSString stringWithFormat:@"%ld", (long)[[NSUserDefaults standardUserDefaults] integerForKey:@"bestScore"]] fontSize:MS_FONT_SIZE_LABEL_SCORE_BEST position:MS_POSITION_LABEL_SCORE_BEST colorByHEX:MS_FONT_COLOR_LABEL_SCORE_BEST andZPosition:MS_ZPOSITION_LABEL_SCORE_BEST]; 

它在屏幕上顯示最佳分數。我該如何獲得這個報告給我設置的排行榜。我的排行榜標識符由我的GlobalSettings.h中的一個雜註標記定義爲我在iTunes Connect上製作的排行榜ID。

我希望這一切都有道理,有人知道如何提供幫助。

回答

0
-(void)reportScore{ 
    if(gameCenterEnabled) 
{ 
int64_t newScore = (int64_t)[[NSUserDefaults standardUserDefaults] integerForKey:@"bestScore"]; 

GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier]; 
score.value = newScore; 

[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) { 
    if (error != nil) { 
     NSLog(@"%@", [error localizedDescription]); 
    } 
}]; 
NSLog(@"Yo we're totally reporting the score now"); 
} 

還增加了NSNotification我GameCenter的代碼

[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportScore) name:@"reportScore" object:nil]; 
1

你得高分提交的GameCenter:

func addLeaderboardScore(score: Int64) { 
    var leaderboardID = "YOURLEADERBOARDID"   
    let newGCScore = GKScore(leaderboardIdentifier: leaderboardID) 
    newGCScore.value = score 
    newGCScore.leaderboardIdentifier = leaderboardID 
    GKScore.reportScores([newGCScore], withCompletionHandler: {(error) -> Void in 
     if error != nil { 
      print("Score not submitted") 
     } 
    }) 
} 
+0

我竟然落得這樣做的東西非常非常相似,你的FUNC哪些工作 – Gabriel

相關問題