Chris Gilardi
Chris Gilardi

Reputation: 1530

GameCenter Integration with SpriteKit

I'm simply trying to log a high score of a single game mode in a game I'm making. I have a leaderboard setup in Game Center on iTunes Connect.

So, my question is, how do I integrate this into my game? I've seen other solutions but can't seem to figure out how they fit into my project.

Thanks!

Upvotes: 0

Views: 347

Answers (1)

jparaskakis
jparaskakis

Reputation: 133

  1. In your view controller implement the GKGameCenterControllerDelegate
  2. Create a local player

    var localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()
    
  3. Authenticate the player in the viewDidLoad with the game center and present the authentication success

    localPlayer.authenticateHandler = {(ViewController, error) -> Void in
        if((ViewController) != nil) {
            self.presentViewController(ViewController, animated: true, completion: nil)
        }
    }
    
  4. Report your score from anywhere in your game

    if (GKLocalPlayer.localPlayer().authenticated) {
      let gkScore = GKScore(leaderboardIdentifier: "YOUR-LEADERBOARD-ID")
      gkScore.value = Int64(YOUR-SCORE)
      GKScore.reportScores([gkScore], withCompletionHandler: ( { (error: NSError!) -> Void in
            if (error != nil) {
                 // handle error
                 println("Error: " + error.localizedDescription);
            } else {
                 println("Score reported: \(gkScore.value)")
            }
        }))
    }
    

Upvotes: 1

Related Questions