所以我试图将游戏中心添加到我的游戏中,但由于它在spriteKit中,因此无法正常工作。

我一直在寻找,并指出,只有在UIViewController中才有可能。

买我所有的按钮(例如进入HighScore(排行榜)的按钮都在spriteKit中...

所以现在我得到这个错误

Could not cast value of type 'UIView' (0x3755c3d8) to 'SKView' (0x36e116cc).


这是我的GameViewController(带有UIViewController的唯一文件),但是它只是用来向我显示TitleScene

class GameViewController: UIViewController, GKGameCenterControllerDelegate {

var gameCenterViewController = GKGameCenterViewController()
var scene : TitleScene?
var gcEnabled = Bool()
var gcDefaultLeaderBoard = String()


func authenticateLocalPlayer() {

    let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()

localPlayer.authenticateHandler = {(GameViewController, error) -> Void in
    if((GameViewController) != nil) {
        // 1 Show login if player is not logged in
        self.presentViewController(GameViewController!, animated: true, completion: nil)
    } else if (localPlayer.authenticated) {
        // 2 Player is already euthenticated & logged in, load game center
        self.gcEnabled = true

        // Get the default leaderboard ID
        localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifer: String?, error: NSError?) -> Void in
            if error != nil {
                print(error)
            } else {
                self.gcDefaultLeaderBoard = leaderboardIdentifer!
            }
        })


    } else {
        // 3 Game center is not enabled on the users device
        self.gcEnabled = false
        print("Local player could not be authenticated, disabling game center")
        print(error)
    }

}

}

func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
    gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
    }
override func viewDidLoad() {
    super.viewDidLoad()

let skView = self.view as! SKView

self.authenticateLocalPlayer()

scene = TitleScene(size: skView.bounds.size)
// Configure the view.

skView.showsFPS = true
skView.showsNodeCount = true

/* Sprite Kit applies additional optimizations to improve rendering performance */
skView.ignoresSiblingOrder = true

/* Set the scale mode to scale to fit the window */
scene!.scaleMode = SKSceneScaleMode.AspectFit
scene!.size = self.view.bounds.size



skView.presentScene(scene)
}


在我的TitleScene中,我得到了

class TitleScene: SKScene {

 if highScoreBtn.containsPoint(location) {
            let ScoreleaderboardID = "ScoreLeaderboardID"
            let sScore = GKScore(leaderboardIdentifier: ScoreleaderboardID)
            sScore.value = Int64(score)

            let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()

            let gcVC: GKGameCenterViewController = GKGameCenterViewController()
            gcVC.gameCenterDelegate = GameViewController()
            gcVC.viewState = GKGameCenterViewControllerState.Leaderboards
            gcVC.leaderboardIdentifier = "LeaderboardID"
            self.view?.window?.rootViewController?.presentViewController(GameViewController(), animated: true, completion: nil)

            GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in
                if error != nil {
                    print(error!.localizedDescription)
                } else {
                    print("Score submitted")

                }
            })


            print("highScoreBtn")
        }

最佳答案

Oke没关系,我找到了它。

这是我的问题的解决方案。

在GameViewController中

class GameViewController : UIViewController {

var scene : TitleScene?

// evrythong moved to TitleScene

}


并在您的TitleScene中这样。

class TitleScene :SKScene, GKGameCenterControllerDelegate {

  var gameCenterViewController = GKGameCenterViewController()
    var gcEnabled = Bool()
    var gcDefaultLeaderBoard = String()

    func authenticateLocalPlayer() {
        let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()

        localPlayer.authenticateHandler = {(TitleScene, error) -> Void in
            if((TitleScene) != nil) {
                // 1 Show login if player is not logged in


// here it was original self.presentViewController(...)
// had to add self.view?.window?.rootController? to it

self.view?.window?.rootViewController?.presentViewController(TitleScene!, animated: true, completion: nil)
                } else if (localPlayer.authenticated) {
                    // 2 Player is already euthenticated & logged in, load game center
                    self.gcEnabled = true



  // Get the default leaderboard ID
                    localPlayer.loadDefaultLeaderboardIdentifierWithCompletionHandler({ (leaderboardIdentifer: String?, error: NSError?) -> Void in
                        if error != nil {
                            print(error)
                        } else {
                            self.gcDefaultLeaderBoard = leaderboardIdentifer!
                        }
                    })


            } else {
                // 3 Game center is not enabled on the users device
                self.gcEnabled = false
                print("Local player could not be authenticated, disabling game center")
                print(error)
            }

        }

    }

    func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
        gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}


didMoveToView函数:

override func didMoveToView(view: SKView) {

    // in the didMoveToView you add the function

    self.authenticateLocalPlayer()

}


并进入排行榜

         if highScoreBtn.containsPoint(location) {
            let ScoreleaderboardID = "ScoreLeaderboardID"
            let sScore = GKScore(leaderboardIdentifier: ScoreleaderboardID)
            sScore.value = Int64(score)

            let localPlayer: GKLocalPlayer = GKLocalPlayer.localPlayer()

            let gcVC: GKGameCenterViewController = GKGameCenterViewController()
            gcVC.gameCenterDelegate = self
            gcVC.viewState = GKGameCenterViewControllerState.Leaderboards
            gcVC.leaderboardIdentifier = "LeaderboardID"

//here i did the same as in with the other self.presentViewController

self.view?.window?.rootViewController?.presentViewController(gcVC, animated: true, completion: nil)

            GKScore.reportScores([sScore], withCompletionHandler: { (error: NSError?) -> Void in
                if error != nil {
                    print(error!.localizedDescription)
                } else {
                    print("Score submitted")

                }
            })


            print("highScoreBtn")
        }

09-29 23:57