[英]How to get high score to submit/reportt to Game Center Leaderboard?
I've scoured the internet for an answer to this. 我已经在网上搜寻了答案。 I have a leaderboard setup in iTunes Connect and it shows up on my game but the high score is never reported to the leaderboard.
我在iTunes Connect中有一个排行榜设置,它会显示在我的游戏中,但从未向该排行榜报告高分。
here is the code I have for the leaderboard in my GameViewController.m 这是我在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];
}
and in my MenuScene.m and EndScene.m I have this code to show the best score which displays on screen as a label. 在我的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];
which displays the best score on the screen. 在屏幕上显示最佳分数。 Hows do I get this to report to the leaderboard I have setup.
如何将其报告给已设置的页首横幅。 My Leaderboard identifier is defined by a pragma mark in my GlobalSettings.h as the the Leaderboard ID I made on iTunes Connect.
我的排行榜标识符由我在GlobalSettings.h中的实用标记定义为我在iTunes Connect上创建的排行榜ID。
I hope all this makes sense and someone knows how to help. 我希望所有这些都有意义,并且有人知道如何提供帮助。
You have to submit the highscore to GameCenter: 您必须将最高分提交给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")
}
})
}
-(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");
}
also added an NSNotification to my GameCenter code 还向我的GameCenter代码添加了NSNotification
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reportScore) name:@"reportScore" object:nil];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.