1

我正在制作一款游戏,玩家可以根据他们做出的选择获得正的高分或负的低分。如果只有一个排行榜,我一直在使用的代码效果很好,但是当我尝试添加第二个时我遇到了麻烦。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 才能让两个排行榜都能正常工作。

4

1 回答 1

2

我在 App Store 上有一款游戏,它有两个排行榜,一个用于积分,一个用于清除等级。我没有为每个排行榜使用一种方法,而是使用了一种方法来提交分数。这是那个方法:

-(void) submitScore:(int64_t)score Leaderboard: (NSString*)leaderboard
{

    //1: Check if Game Center
    //   features are enabled
    if (!_gameCenterFeaturesEnabled) {
        return;
    }

    //2: Create a GKScore object
    GKScore* gkScore =
    [[GKScore alloc]
     initWithLeaderboardIdentifier:leaderboard];

    //3: Set the score value
    gkScore.value = score;

    //4: Send the score to Game Center
    [gkScore reportScoreWithCompletionHandler:
     ^(NSError* error) {

         [self setLastError:error];

         BOOL success = (error == nil);

         if ([_delegate
              respondsToSelector:
              @selector(onScoresSubmitted:)]) {

             [_delegate onScoresSubmitted:success];
         }
     }];


}

当你想提交你的高分时,你所要做的就是添加如下内容:

[[GCHelper sharedGameKitHelper] submitScore:myLowScore Leaderboard:TELS];
[[GCHelper sharedGameKitHelper] submitScore:myHighScore Leaderboard:TEHS];

GCHelper 是包含我的 submitScore:Leaderboard: 方法的类。

要在您的应用程序中查看您的排行榜或成就,请尝试以下操作:

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

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

- (void) presentAchievements {
    GKGameCenterViewController* gameCenterController = [[GKGameCenterViewController alloc] init];
    gameCenterController.viewState = GKGameCenterViewControllerStateAchievements;
    gameCenterController.gameCenterDelegate = self;

我希望这回答了你的问题!

于 2014-07-14T20:07:45.433 回答