2012-04-13 227 views
0

我一直在努力工作在遊戲中心。我測試了很多我已經失去的代碼。 我很想知道如何自動提交分數以及 這裏是我已經使用了一些代碼,但我不知道這是否會幫助遊戲中心? Xcode

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

回答

1

要報告您需要使用GKScore如下得分;

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

     } 
}]; 

上面的代碼分配和使用inits您已經成立了遊戲中心,你想舉報的得分類別的標識符的GKScore對象。您更新分數的值,然後使用reportScoreWithCompletionHandler確保測試錯誤,以便您可以將分數歸檔並稍後進行報告。