1

Is it possible to set up a Game Center leaderboard so it the "score" is persistent ? I want to use leaderboards to show the number of wins a player has, but I don't see how it's possible to just increase a counter with a leaderboard, is that possible?

4

1 回答 1

0

您可以使用NSUserDefaults保存和检索数据(获胜次数)
示例:

//ViewController.h :    
NSInteger NbrOfWins ; 




//ViewController.m :
- (void)viewDidLoad
{
//...
[super viewDidLoad];
// ...

//Retrieving data
NbrOfWins = [[NSUserDefaults standardUserDefaults] integerForKey:@"Wins"];

}




-(void)NewWin{
//...

NbrOfWins = NbrOfWins + 1 ;

//Saving data
[[NSUserDefaults standardUserDefaults]setInteger:NbrOfWins forKey:@"Wins"];

//Calling a Game Center function to send the new number of wins :
[self reportScore];
}



//Game center functions 
-(void)reportScore{
// Create a GKScore object to assign the score and report it as a NSArray object.
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:_leaderboardIdentifier];

//Sending the new number of wins :
score.value = NbrOfWins;

[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
    if (error != nil) {
        NSLog(@"%@", [error localizedDescription]);
    }
  }];
}

// ... Other game center functions please see the documentation

有关 Game Center 实施的更多信息:http:
//www.appcoda.com/ios-game-kit-framework/

有关NSUserDefaults 的更多信息:http :
//www.icodeblog.com/2008/10/03/iphone-programming-教程保存检索数据使用nsuserdefaults/

于 2014-05-30T22:53:44.913 回答