2

I made a game using SpriteKit and Xcode 7 beta. I tried to add GameCenter and Leaderboard but the problem is that the score in the leaderboard won't change (HighScore doesn't upload to GameCenter), It stay 0 all the time and I don't know how to fix it. I'm using 2 different files: GameViewController.swift, and PointsLabel.swift

GameViewController.swift:

import GameKit

class GameViewController: UIViewController,UIGestureRecognizerDelegate, GKGameCenterControllerDelegate {

var score: PointsLabel!

override func viewDidLoad() {
    super.viewDidLoad()

    //initiate gamecenter
func authenticateLocalPlayer(){

    let localPlayer = GKLocalPlayer.localPlayer()

    localPlayer.authenticateHandler = {(GameViewController, error) -> Void in

        if (GameViewController != nil) {
            self.presentViewController(GameViewController!, animated: true, completion: nil)
        }

        else {
            print((GKLocalPlayer.localPlayer().authenticated))
        }
     }
  }
}

@IBAction func leaderboard(sender: UIButton) {
    saveHighscore(score)
    showLeader()
}


//send high score to leaderboard
func saveHighscore(score:Int) {

    //check if user is signed in
    if GKLocalPlayer.localPlayer().authenticated {

        let scoreReporter = GKScore(leaderboardIdentifier: "Leaderboard_01")

        scoreReporter.value = Int64(score)

        let scoreArray: [GKScore] = [scoreReporter]

        GKScore.reportScores(scoreArray, withCompletionHandler: {error -> Void in
            if error != nil {
                print("error")
            }
        })
    }
}


    //shows leaderboard screen
    func showLeader() {
        let vc = self.view?.window?.rootViewController
        let gc = GKGameCenterViewController()
        gc.gameCenterDelegate = self
        vc?.presentViewController(gc, animated: true, completion: nil)
    }
}

//hides leaderboard screen
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController)
{
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)

}

There is an Error in this file:

Cannot invoke 'saveHighscore' with an argument list of type '(PointsLabel!)'

on code:

@IBAction func leaderboard(sender: UIButton) {
    saveHighscore(score) //<- Here is Error
    showLeader()
}

PointsLabel.swift:

import Foundation
import UIKit
import SpriteKit

class PointsLabel: SKLabelNode {

var score:Int = 0

init(num: Int) {
    super.init()

    fontColor = UIColor.blackColor()
    fontSize = 30.0

    score = num
    text = "\(num)"
}

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

func increment() {
    score++
    text = "\(score)"
}

func setTo(num: Int) {
    self.score = num
    text = "\(self.score)"
 }
}

I don't know how to fix it!

Emm
  • 1,963
  • 2
  • 20
  • 51
  • Your `score` variable is of type `PointsLabel` but your `saveHighscore` function expects an `Int` as parameter. – Eric Aya Aug 30 '15 at 12:04
  • So how I need to call the class PointsLabel.swift on class GameViewController.swift, Because as I show on question score is in class PointsLabel.swift and there is as Int ? – Emm Aug 30 '15 at 12:06

1 Answers1

1

Your score variable is of type PointsLabel but your saveHighscore function expects an Int as parameter.

Looking at your code, the variable score will be an instance of PointsLabel so I guess you could use the score property of your instanciated class, which is an Int (the fact that you used "score" as a name for both variables is confusing. I suggest changing names to make them more explicit.).

@IBAction func leaderboard(sender: UIButton) {
    saveHighscore(score.score)
    showLeader()
}
Eric Aya
  • 69,473
  • 35
  • 181
  • 253
  • I did as You said, I ran simulator and when I press button of leaderboard the simulator stops and it found error on same code: fatal error: unexpectedly found nil while unwrapping an Optional value – Emm Aug 30 '15 at 12:14
  • Well, it's probably because `score` is nil and it crashes because you declared it as an implicitly unwrapped optional. Give it a value and it'll work. // I gave you the explanation and solution for the specific problem you asked in your question, now you have to debug other problems yourself, I can't do it for you. :) If you get stuck with a new problem, ask a new question. – Eric Aya Aug 30 '15 at 12:17
  • I think that it have a value, anyway thanks for your help – Emm Aug 30 '15 at 12:21
  • 1
    Don't think: *test*. :) Print the values. Set breakpoints. Debug. Follow the logic of the code step by step. Get rid of forced unwrapping, use safe unwrapping and error handling. Etc. – Eric Aya Aug 30 '15 at 12:33