我正在制作一款游戏,玩家可以根据他们做出的选择获得正的高分或负的低分。如果只有一个排行榜,我一直在使用的代码效果很好,但是当我尝试添加第二个时我遇到了麻烦。TEHS 是 HighScore 的排行榜标识符,TELS 是 LowScore 的排行榜标识符。
我验证本地播放器:
-(void)authenticateLocalPlayer{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController != nil) {
[self presentViewController:viewController animated:YES completion:nil];
}
else{
if ([GKLocalPlayer localPlayer].authenticated) {
_gameCenterEnabled = YES;
// Get the default leaderboard identifier.
[[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
else{
TEHS = leaderboardidentifier;
}
}];
}
else{
_gameCenterEnabled = NO;
}
}
};
}
报告分数:
-(void)reportHighScore{
GKScore *highscore = [[GKScore alloc] initWithLeaderboardIdentifier:TEHS];
highscore.value = HighScoreNumber;
[GKScore reportScores:@[highscore] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}
-(void)reportLowScore {
GKScore *lowscore = [[GKScore alloc] initWithLeaderboardIdentifier:TELS];
lowscore.value = LowScoreNumber;
[GKScore reportScores:@[lowscore] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}
并且,当玩家激活时会显示排行榜:
-(void)showLeaderboardAndAchievements:(BOOL)shouldShowLeaderboard{
GKGameCenterViewController *gcViewController = [[GKGameCenterViewController alloc] init];
gcViewController.gameCenterDelegate = self;
if (shouldShowLeaderboard) {
gcViewController.viewState = GKGameCenterViewControllerStateLeaderboards;
gcViewController.leaderboardIdentifier = TEHS;
}
else{
gcViewController.viewState = GKGameCenterViewControllerStateAchievements;
}
[self presentViewController:gcViewController animated:YES completion:nil];
}
当我更改时,reportLowScore 或 reportHighScore 都可以使用
gcViewController.leaderboardIdentifier = TEHS;
和
TEHS = 排行榜标识符;
以匹配他们各自的标识符。所以,现在,这段代码适用于高分(TEHS),如果我把上面的两个改成 TELS,低分就可以了。我只是不太确定我需要做些什么来验证LocalPlayer 和 showLeaderboardAndAchievements 才能让两个排行榜都能正常工作。