【问题标题】:Game Center Sandbox mode display multiple leaderboards游戏中心沙盒模式显示多个排行榜
【发布时间】:2014-04-09 21:39:06
【问题描述】:

我正准备推出我的第一个应用,并希望在我的游戏中拥有多个排行榜。目前在沙盒模式下,我可以成功跟踪并将分数记录到游戏中心。 Game Center 保存了我的分数(仅当它更高时)并且似乎功能齐全。

我知道,通过 Itunes Connect,我们可以设置多个排行榜,这看起来很简单。不过,我仍然希望能够在发布我的游戏之前测试多个排行榜。有没有办法在沙盒模式下做到这一点?目前,我的分数似乎只会自动登录到默认排行榜。下面是我用来保存/访问分数的相关代码。谢谢!

ABGameKitHelper.m

#pragma mark - Leaderboard
-(void) reportScore:(long long)aScore forLeaderboard:(NSString*)leaderboardId
{
    GKScore *score = [[GKScore alloc] initWithCategory:leaderboardId];
    score.value = aScore;

    [score reportScoreWithCompletionHandler:^(NSError *error) {
        if (!error)
        {
            if(![self hasConnectivity])
            {
                [self cacheScore:score];
            }
            if (ABGAMEKITHELPER_LOGGING) NSLog(@"ABGameKitHelper: Reported score (%lli) to %@ successfully.", score.value, leaderboardId);
        }
        else
        {
            [self cacheScore:score];
            if (ABGAMEKITHELPER_LOGGING) NSLog(@"ABGameKitHelper: ERROR -> Reporting score (%lli) to %@ failed, caching...", score.value, leaderboardId);
        }
    }];
}

-(void) showLeaderboard:(NSString*)leaderboardId
{
    GKLeaderboardViewController *viewController = [GKLeaderboardViewController new];
    viewController.leaderboardDelegate = self;
    if (leaderboardId)
    {
        viewController.category = leaderboardId;
        CCLOG(@"Going to category already created");
    }

    [[self topViewController] presentViewController:viewController animated:YES completion:nil];
}

MainScene.m

- (void)gameCenter {
    [[ABGameKitHelper sharedHelper] reportScore:1400 forLeaderboard:@"Score"];
    [[ABGameKitHelper sharedHelper] showLeaderboard:@"Score"];
}

【问题讨论】:

    标签: ios cocos2d-iphone game-center


    【解决方案1】:

    我不确定我是否正确理解了您的问题,但我会尽力回答! Game Center 确实支持多个排行榜:

    -如果你想发送分数到特定排行榜,你只需要调用函数[[ABGameKitHelper sharedHelper] reportScore:X forLeaderboard:LEADERBOARD_ID];,其中X代表你要发送的分数,LEADERBOARD_ID 是您想要将分数发送到的排行榜的 ID,如 iTunes Connect 中所指定。

    -当您有多个排行榜时,如果您不想只显示一个排行榜,而是显示所有排行榜,您应该使用GKGameCenterViewController 类。但是,要小心;此 ViewController 仅在 iOS 6 中添加,因此您必须检查设备运行的是哪个版本。我也在使用 ABGameKitHelper,所以我制作了一个函数来显示这种视图。就是这样:

    ABGameKitHelper.m

    - (void) showGameCenter{
        if (![[ABGameKitHelper sharedHelper] hasConnectivity]) return;
    
        //Check if device runs on iOS 5
        if([[[UIDevice currentDevice]systemVersion]intValue]==5)
        {
            //If so, we must use the GKLeaderboardViewController
            GKLeaderboardViewController *leaderboard = [[GKLeaderboardViewController alloc] init];
    
            if (leaderboard != nil)
            {
                leaderboard.leaderboardDelegate = self;
                [[self topViewController] presentViewController:leaderboard animated:YES completion:nil];
            }
    
        }else if ([[[UIDevice currentDevice]systemVersion]intValue]>=6)
        {
            //if it runs on iOS 6 or higher, we use GKGameCenterViewController
            GKGameCenterViewController *gameCenterController = [[GKGameCenterViewController alloc] init];
    
            if (gameCenterController != nil)
            {
                gameCenterController.gameCenterDelegate = self;
                gameCenterController.viewState = GKGameCenterViewControllerStateDefault;
    
                [[self topViewController] presentViewController:gameCenterController animated:YES completion:nil];
            }
        }
    }
    

    别忘了添加:

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

    使用此功能可以让您显示包含所有排行榜和成就的漂亮视图。

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多