Domanda

I'm preparing to launch my first app and want to have multiple leaderboards inside my game. Currently in sandbox mode I can track and log scores into Game Center successfully. Game Center saves my scores (only if it is higher) and seems to be fully functional.

I know through Itunes Connect we have the ability to set up multiple leaderboards and it seems pretty straight forward. I still want to be able to test multiple leaderboards before publishing my game though. Is there a way to do this in sandbox mode? Currently it seems like my scores are only automatically logged into a default leaderboard. Below is the relevant code I'm using to save/access scores. Thanks!

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"];
}
È stato utile?

Soluzione

I'm not sure if I understand your question properly, but I'll try to answer! Game Center does support multiple leaderboards:

-If you want to send a score to specific leaderboard, you just have to call the function [[ABGameKitHelper sharedHelper] reportScore:X forLeaderboard:LEADERBOARD_ID];, where X represents the score you'd like to send, and LEADERBOARD_ID is the ID of the leaderboard you want to send the score to, as specified in iTunes Connect.

-When you have multiple leaderboards, if you don't want to show just one leaderboard, but a list of them all, you should use the GKGameCenterViewController class instead. However, be careful; this ViewController has been added in iOS 6 only, so you must check which version the device is running. I am also using the ABGameKitHelper, so I've made a function to show this kind of view. Here it goes :

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];
        }
    }
}

And don't forget to add :

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

Using this function will allow you to show a nice view containing all your leaderboards and achievements.

Hope this helps!

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top