编码第二个排行榜

时间:2014-07-07 20:21:23

标签: ios objective-c game-center leaderboard game-center-leaderboard

我正在制作一款游戏,根据玩家的选择,玩家可以获得正面的高分或负的低分。如果只有一个排行榜,我使用过的代码效果很好,但是当我尝试添加第二个排行榜时,我遇到了麻烦。 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 = leaderboardidentifier;

匹配各自的标识符。所以,现在,这个代码适用于高分(TEHS),如果我将上述两个改为TELS,那么低分会起作用。我不太确定我需要做什么来验证LocalPlayer和showLeaderboardAndAchievements以使两个排行榜都能正常工作。

1 个答案:

答案 0 :(得分: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:排行榜:方法的类。

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

- (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;

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