Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I implement GameCenter Leaderboard? [closed]

I haven't been able to follow other tutorials and answers on Stack Overflow, so how do I implement GameCenter Leaderboards in my game, in SpriteKit in Swift? What is the code that one would use, and would it go in the GameScene or the GameViewController? I have already set up a leaderboard on iTunes Connect. How would I go about this? Thanks!

like image 854
tdh Avatar asked Feb 12 '23 02:02

tdh


1 Answers

First you must authenticate the player:

 var localPlayer = GKLocalPlayer.localPlayer()
    localPlayer.authenticateHandler = {(viewController : UIViewController!, error : NSError!) -> Void in
        if ((viewController) != nil) {
            self.presentViewController(viewController, animated: true, completion: nil)
        }else{

            println((GKLocalPlayer.localPlayer().authenticated))
        }
    }

Then you can open the leaderboard:

 func showLeaderboard() {


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

        gcViewController.viewState = GKGameCenterViewControllerState.Leaderboards


        gcViewController.leaderboardIdentifier = "YOUR_BOARD_NAME"

        self.showViewController(gcViewController, sender: self)
        self.navigationController?.pushViewController(gcViewController, animated: true)

    }
    func gameCenterViewControllerDidFinish(gcViewController: GKGameCenterViewController!)
    {

        self.dismissViewControllerAnimated(true, completion: nil)
    }

I suggest doing these in your view controller. Authenticate the player on viewDidAppear though not viewDidLoad

like image 118
Kendel Avatar answered Feb 13 '23 21:02

Kendel