在SpriteKit中显示一个游戏中心排行榜

时间:2023-01-22 23:17:58

I'm working on a game, and I'm looking for help or the code to display a GameCenter leaderboard in my app when a user clicks a button. I have no clue where to start as all of the other answers seem to be for Obj-C, thanks!

我正在开发一个游戏,我在寻找帮助或代码,当用户单击按钮时,在我的应用程序中显示GameCenter排行榜。我不知道从哪里开始,因为其他的答案似乎都是针对object - c的,谢谢!

EDIT: The Below answer worked perfectly, but for those wondering how to do this within SpriteKit, simply add the below methods to the GameViewController and add a Notification Center Observer

编辑:下面的答案工作得很好,但是对于那些想在SpriteKit中实现这一点的人来说,只需将下面的方法添加到GameViewController中,并添加一个通知中心观察者

NSNotificationCenter.defaultCenter().addObserver(self, selector: "showLeaderboard", name: "showLeaderboard", object: nil)

In your SKScene Class, simply call to that observer.

在SKScene类中,只需调用该观察者。

NSNotificationCenter.defaultCenter().postNotificationName("showLeaderboard", object: nil)

Just to help out those wondering!

只是为了帮助那些好奇的人!

2 个解决方案

#1


0  

Include the GKGameCenterControllerDelegate protocol within your class.

在类中包含GKGameCenterControllerDelegate协议。

class ViewController: UIViewController, GKGameCenterControllerDelegate

This method dismisses the Game Center view when "Done" is tapped:

当“完成”被点击时,该方法取消游戏中心视图:

func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

This function includes the code that is needed to display the leaderboard:

此功能包括显示排行榜所需的代码:

func showLeaderboard() {

    // declare the Game Center viewController
    var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    // Remember to replace "Best Score" with your Leaderboard ID (which you have created in iTunes Connect)
    gcViewController.leaderboardIdentifier = "Best_Score"
    // Finally present the Game Center ViewController
    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
    self.presentViewController(gcViewController, animated: true, completion: nil)
}

You can now trigger the function showLeaderboard by pressing a UIButton:

您现在可以通过按下UIButton来触发函数showLeaderboard:

@IBAction func buttonShowLeaderboard(sender: AnyObject) {
    showLeaderboard()
}

#2


1  

You can do it like CeceXX showed, or you can use Easy-Game-Center by DaRkD0G to make it easier. https://github.com/DaRkD0G/Easy-Game-Center-Swift

你可以像CeceXX展示的那样做,也可以使用DaRkD0G的easy - game center来简化操作。https://github.com/DaRkD0G/Easy-Game-Center-Swift

#1


0  

Include the GKGameCenterControllerDelegate protocol within your class.

在类中包含GKGameCenterControllerDelegate协议。

class ViewController: UIViewController, GKGameCenterControllerDelegate

This method dismisses the Game Center view when "Done" is tapped:

当“完成”被点击时,该方法取消游戏中心视图:

func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!) {
    self.dismissViewControllerAnimated(true, completion: nil)
}

This function includes the code that is needed to display the leaderboard:

此功能包括显示排行榜所需的代码:

func showLeaderboard() {

    // declare the Game Center viewController
    var gcViewController: GKGameCenterViewController = GKGameCenterViewController()
    gcViewController.gameCenterDelegate = self

    gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards

    // Remember to replace "Best Score" with your Leaderboard ID (which you have created in iTunes Connect)
    gcViewController.leaderboardIdentifier = "Best_Score"
    // Finally present the Game Center ViewController
    self.showViewController(gcViewController, sender: self)
    self.navigationController?.pushViewController(gcViewController, animated: true)
    self.presentViewController(gcViewController, animated: true, completion: nil)
}

You can now trigger the function showLeaderboard by pressing a UIButton:

您现在可以通过按下UIButton来触发函数showLeaderboard:

@IBAction func buttonShowLeaderboard(sender: AnyObject) {
    showLeaderboard()
}

#2


1  

You can do it like CeceXX showed, or you can use Easy-Game-Center by DaRkD0G to make it easier. https://github.com/DaRkD0G/Easy-Game-Center-Swift

你可以像CeceXX展示的那样做,也可以使用DaRkD0G的easy - game center来简化操作。https://github.com/DaRkD0G/Easy-Game-Center-Swift

相关文章