JohnAnge Kernodle
JohnAnge Kernodle

Reputation: 259

Game Center? Xcode

I have been working ver hard on Game center. I have tested so many codes I've lost count. I would love to know how to automatically submit score as well here are some codes i have used but i am not sure if this will help

-(IBAction)showleaderboard:(id)sender{
GKLeaderboardViewController *leaderboardController = [[GKLeaderboardViewController   alloc]init];
if (leaderboardController !=NULL) {
    leaderboardController.category = self.currentLeaderboard;
    leaderboardController.timeScope = GKLeaderboardTimeScopeAllTime;
    leaderboardController.leaderboardDelegate = self;
    [self presentModalViewController:leaderboardController animated:YES];
}

}
-(void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController{
[self dismissModalViewControllerAnimated:YES];
[viewController release];

}


-(IBAction)showAchivementLeaderboard:(id)sender{
GKAchievementViewController *achivements = [[GKAchievementViewController alloc]init];
if (achivements !=NULL) {
    achivements.achievementDelegate = self;
    [self presentModalViewController:achivements animated:YES];
}
}
-(void)achievementViewControllerDidFinish:(GKAchievementViewController *)viewController{
[self dismissModalViewControllerAnimated:YES];
[viewController release];

}

self.currentLeaderboard= kEasyLeaderboardID;
if ([gameCenterManager isGameCenterAvailible]) {
   self.gameCenterManager= [[[GameCenterManager alloc] init] autorelease];
    [self.gameCenterManager setDelegate:self];
    [self.gameCenterManager authenticateLocalUser];
}else{

    UIAlertView *openURLAlert = [[ UIAlertView alloc] initWithTitle:@"Game Center turned    off" message:@"You are not connected to game center." delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [openURLAlert show];
    [openURLAlert release];
}

Upvotes: 0

Views: 623

Answers (1)

Des Cullen
Des Cullen

Reputation: 121

To report a score you need to use GKScore as follows;

GKScore *scoreReporter = [[GKScore alloc] initWithCategory:self.gameCategory.leaderboardString];
scoreReporter.value = score;

[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) {
    if (error != nil) {

        [[KYTGlobals instance] storeScore:score forCategory:self.gameCategory.leaderboardString];

        }
 }];

The above code allocates and inits a GKScore object using the identifier that you have already set up on game center for the category that you want to report a score for. You update the value for the score and then use reportScoreWithCompletionHandler making sure to test for error so that you can archive the score and report it later.

Upvotes: 1

Related Questions