如何获得高分数提交/报告给游戏中心排行榜?

时间:2016-02-19 15:35:03

标签: ios objective-c sprite-kit game-center

我已经在互联网上搜索了这个答案。我在iTunes Connect中设置了排行榜设置,它显示在我的游戏中,但高分从未报告给排行榜。

这是我在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。

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

2 个答案:

答案 0 :(得分: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")
        }
    })
}

答案 1 :(得分: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");
}

还在我的GameCenter代码中添加了NSNotification

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