【问题标题】:How to implement a leaderboard (Game Center) into project如何在项目中实现排行榜(游戏中心)
【发布时间】:2015-02-25 17:01:23
【问题描述】:

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

【问题讨论】:

标签: ios objective-c leaderboard


【解决方案1】:

如果你真的看过这个,你会发现很多关于这个的教程,所以我认为你没有,或者你还没有真正学会如何使用 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 是您在代码中输入的名称,例如我在上面提到了我的排行榜名称。完成此操作后,您将返回您的应用程序并向下滚动,直到找到游戏中心,然后单击加号并添加选定的排行榜。但是,如果您甚至不知道如何将应用程序添加到 iTunes Connect,那么您应该认真阅读整篇文章,例如这里是了解 how. 的好地方@ 这里是了解如何理解 game centre. 的好地方

【讨论】:

  • 我已经在 iTunes Connect 中创建了排行榜,所以我有一个排行榜 ID。我只是还没有找到关于如何正确编码的教程,它们不是最新的。如何让用户登录游戏中心?正如您在上面提供的代码(在 viewDidLoad 中)中看到的那样,该代码已被弃用。以及打开排行榜的代码是什么?
  • 我知道,但运行它会完美运行,我目前在 App Store 上的游戏中使用该方法!
  • Michael,我刚刚注意到,当我在真实设备上测试我的应用程序时,我没有登录到 Game Center。两台设备都运行 iOS 8.1.2(最新),如果这与它有关...您在哪个应用程序中使用此代码?
  • 代码已更新,如果您转到设置然后转到游戏中心,您可能没有登录,因此请检查您是否以这种方式登录。
  • 我已登录。但是,我在 Developer 下的同一屏幕底部启用了 Sandbox。现在,当我启动应用程序时,它要求我登录 Game Center,就像在模拟器中一样。这是它的工作方式吗?人们可以自动登录吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-10
相关资源
最近更新 更多