斯威夫特 - 无法让游戏中心显示出来

时间:2014-08-12 23:32:04

标签: ios xcode delegates swift game-center

我正在使用此代码在用户按下按钮时显示 Game Center 。我希望这能在排行榜上显示出高分:

@IBAction func showScores(x : UIButton) {
    var gameCenterController : GKGameCenterViewController!
    if (gameCenterController != nil) {
        gameCenterController.gameCenterDelegate = self
        self.presentViewController(gameCenterController, animated: true, completion: nil)
    }
}

但我在这一行上收到错误:

gameCenterController.gameCenterDelegate = self

那就读了,

 Type 'MenuViewController' does not conform to protocol `GKGameCenterControllerDelegate`

我做错了什么?

如何将GameCenter实施到我的游戏中,以便将用户的高分添加到排行榜中,只需按一下按钮即可查看?

2 个答案:

答案 0 :(得分:10)

确保声明您的演示视图控制器符合GKGameCenterControllerDelegate,如下所示:

class MenuViewController: GKGameCenterControllerDelegate

然后在Swift中,将以下函数添加到MenuViewController:

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
{
    //code to dismiss your gameCenterViewController
    // for example:
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}

最后,确保在游戏中心视图控制器上显示之前设置了gameCenterDelegate。例如:

var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
gcViewController.gameCenterDelegate = self

请注意,代理人是gameCenterDelegate,而不只是delegate

此外,这是一个如何从MenuViewController显示您的排行榜的示例。 (此代码将在MenuViewController中):

func showLeaderboard()
{
    var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards
    gcViewController.leaderboardIdentifier = "yourleaderboardid"

    self.presentViewController(gcViewController, animated: true, completion: nil)
}

答案 1 :(得分:3)

它表示你没有实现委托方法。

// Called when the player is done interacting with the view controller’s content. (required)
- (void)gameCenterViewControllerDidFinish:(GKGameCenterViewController *)gameCenterViewController

然后你的课可以这样实现:

class YourViewController : GKGameCenterControllerDelegate
{
    func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController!)
    {
        // do somthing
    }
}