问题
Right now I am trying to make a leaderboard I created show up. The player is authenticated just fine, but when the game center window opens it is very strange. Here is a picture:
Here is the code I am using to display this image:
override func touchesBegan(touches: NSSet, withEvent event: UIEvent) {
self.showLeaderboard()
}
func showLeaderboard() {
var leaderView = UIViewController()
var leaderViewController = GKGameCenterViewController(rootViewController: leaderView)
leaderViewController.viewState = GKGameCenterViewControllerState.Leaderboards
leaderViewController.leaderboardIdentifier = "High_Score_Board"
self.showViewController(leaderViewController, sender: self)
//self.presentViewController(leaderViewController, animated: true, completion: nil)
}
func leaderboardViewControllerDidFinish(controller: GKGameCenterViewController){
controller.dismissViewControllerAnimated(true, completion: nil)
}
All of this is in my GameViewController. Also, even if this works, how would I access this method in my SKScenes? Thanks for the help!
回答1:
Import GameKit:
import GameKit
Make sure to add the GKGameCenterControllerDelegate
delegate within your class.
class ViewController: UIViewController, GKGameCenterControllerDelegate {
...
}
That delegate requires a method which is called when the player taps on the "Done" button.
func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
{
self.dismissViewControllerAnimated(true, completion: nil)
}
This is the function that includes the code needed to display the leaderboard:
func showLeaderboard() {
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"
self.showViewController(gcViewController, sender: self)
self.navigationController?.pushViewController(gcViewController, animated: true)
// self.presentViewController(gcViewController, animated: true, completion: nil)
}
You can now call showLeaderboard
by pressing a button:
@IBAction func buttonShowLeaderboard(sender: AnyObject) {
showLeaderboard()
}
来源:https://stackoverflow.com/questions/27749349/swift-displaying-game-center-leaderboards