设置游戏中心排行榜,这是一个计数器

时间:2014-05-30 21:52:12

标签: ios objective-c game-center game-center-leaderboard

是否可以设置游戏中心排行榜以使“得分”持续存在?我想使用排行榜来显示玩家拥有的胜利数量,但我不知道如何通过排行榜增加一个计数器,这可能吗?

1 个答案:

答案 0 :(得分: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-tutorial-savingretrieving-data-using-nsuserdefaults/