如何将排行榜(Game Center)实施到项目中

时间:2014-12-29 13:43:00

标签: ios objective-c leaderboard

我正在尝试在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"); } }];

1 个答案:

答案 0 :(得分:2)

如果你真的看过这个,你会发现大量的教程,所以我不认为你有,或者你还没有真正学会如何使用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连接,那么您应该认真阅读整篇文章,例如这里是一个找到 how. 的好地方。这里很好可以了解如何理解game centre.