我试图在Objective-C中创建一个排行榜,但是没有运气。我一直在这里和其他站点四处寻找,但似乎找不到任何有效的方法。我已经在iTunes Connect上创建了排行榜,但这是我遇到的代码。我收到警告,某些术语已过时。
这是我使用的代码:
- (IBAction)ShowLeaderboard:(id)sender {
GKGameCenterViewController *leaderboardController = [[GKGameCenterViewController alloc] init];
if (leaderboardController != nil) { leaderboardController.leaderboardCategory = self;
[self presentModalViewController: leaderboardController animated: YES]; }
}
- (IBAction)SubmitScoreToGameCenter:(id)sender {
GKScore *scoreReporter = [[GKScore alloc] initWithCategory:@"LeaderboardName"];
scoreReporter.value = HighScoreNumber;
[scoreReporter reportScoreWithCompletionHandler:^(NSError *error) { if (error != nil){ NSLog(@"Submitting score failed"); }
else { NSLog(@"Submitting score succeeded"); } }];
}
- (void)leaderboardViewControllerDidFinish:(GKLeaderboardViewController *)viewController
{ [self dismissModalViewControllerAnimated:YES];
}
而这在viewDidLoad中:
{
[[GKLocalPlayer localPlayer] authenticateWithCompletionHandler:^(NSError *error) { if (error == nil)
{ NSLog(@"Authentication Successful");
}
else { NSLog(@"Authentication Failed"); } }];
最佳答案
如果您实际进行过查找,则会发现很多与此相关的教程,所以我认为您没有,或者您还没有真正学习过如何使用xCode。
这是我做代码的方法:
-(void)submitScore //submit the score to game centre
{
GKScore *score = [[GKScore alloc] initWithLeaderboardIdentifier:@"LeaderboardName"];
int64_t GameCenterScore = Score;
score.value = GameCenterScore;
[GKScore reportScores:@[score] withCompletionHandler:^(NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
}];
}
-(void)authentication //log player into game centre
{
GKLocalPlayer *localPlayer = [GKLocalPlayer localPlayer];
localPlayer.authenticateHandler = ^(UIViewController *viewController, NSError *error){
if (viewController != nil) {
[self presentViewController:viewController animated:YES completion:nil];
}
else{
if ([GKLocalPlayer localPlayer].authenticated) {
NSLog(@"authentication succcesful");
GameCenterAvaliable = YES;
[[GKLocalPlayer localPlayer] loadDefaultLeaderboardIdentifierWithCompletionHandler:^(NSString *leaderboardIdentifier, NSError *error) {
if (error != nil) {
NSLog(@"%@", [error localizedDescription]);
}
else{
leaderboardIdentifier = leaderboardIdentifier;
}
}];
}
else{
NSLog(@"authentication unseuccseful");
GameCenterAvaliable = NO;
}
}
};
}
-(IBAction)ShowGameCenter:(id)sender //show game centre
{
GKLeaderboardViewController *LeaderboardController = [[GKLeaderboardViewController alloc] init];
if (LeaderboardController != nil) {
LeaderboardController.leaderboardDelegate = self;
[self presentViewController:LeaderboardController animated:YES];
}
}
//Animate gc out if finished with it
-(void)leaderboardViewControllerDidFinish: (GKLeaderboardViewController *) viewController{
[self dismissViewControllerAnimated:YES];
}
那是为了将分数发送到排行榜,但我认为您在设置排行榜时遇到了麻烦?是?
从iTunes Connect转到您的应用程序,从那里转到您想要排行榜的应用程序,然后从那里转到游戏中心。单击添加排行榜,并选择一个排行榜,然后填写信息。排行榜ID是您在代码中输入的名称,例如我在上面已经将我的LeaderBoard命名为。完成此操作后,您可以返回到您的应用并向下滚动直到找到游戏中心,然后单击加号并添加选定的排行榜。但是,如果您甚至不知道如何在iTunes connect中添加应用程序,都应该认真阅读整篇文章,例如,这里是找出 how. 的好地方,这里也是了解如何理解game centre.的好地方
关于ios - 如何在项目中实现排行榜(游戏中心),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/27690054/