I've tried a whole bunch of ways to get Game Center working in my SpriteKit
game. Unfortunately the way I've done it in the past using ObjC and ViewControllers don't work because I'm using SKScene
/ a GameScene.
我已经尝试了很多方法让Game Center在我的SpriteKit游戏中运行。不幸的是,我过去使用ObjC和ViewControllers的方式不起作用,因为我使用的是SKScene / GameScene。
This is the swift version of the code (I think):
这是代码的快速版本(我认为):
// MARK: Game Center Integration
//login and make available
func authenticateLocalPlayer(){
let localPlayer = GKLocalPlayer()
print(localPlayer)
localPlayer.authenticateHandler = {(viewController, error) -> Void in
if ((viewController) != nil) {
self.presentViewController(viewController!, animated: true, completion: nil)
}else{
print((GKLocalPlayer.localPlayer().authenticated))
}
}
}
//submit a score to leaderboard
func reportScoreToLeaderboard(thisScore:Int){
if GKLocalPlayer.localPlayer().authenticated {
let scoreReporter = GKScore(leaderboardIdentifier: "LeaderboardID")
scoreReporter.value = Int64(thisScore)
let scoreArray: [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: { (error: NSError?) -> Void in
if error != nil {
print(error!.localizedDescription)
} else {
print("Score submitted")
}
})
}
}
//show leaderboard (call from button or touch)
func showLeaderboard() {
let vc = self.view?.window?.rootViewController
let gc = GKGameCenterViewController()
gc.gameCenterDelegate = self
vc?.presentViewController(gc, animated: true, completion: nil)
}
//hides view when finished
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController){
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
Unfortunetely, no matter what I try, I either get this error:
不幸的是,无论我尝试什么,我都会得到这个错误:
Attempting to load the view of a view controller while it is deallocating is not allowed and may result in undefined behavior
尝试在取消分配时加载视图控制器的视图是不允许的,并且可能导致未定义的行为
... or it just crashes.
......或者只是崩溃
I've read that NSNotifications
can be used? But how?
我读过可以使用NSNotifications吗?但是怎么样?
I'm guessing the best way is to set it all up in the GameViewController.swift
and use NSNotifications
to communicate with the RootViewController from the GameScene? I can't seem to find a tutorial or example though.
我猜最好的方法是在GameViewController.swift中设置它并使用NSNotifications从GameScene与RootViewController进行通信?我似乎无法找到教程或示例。
1 个解决方案
#1
1
Use delegation when a view controller needs to change views, do not have the view itself change views, this could cause the view trying to deallocating while the new view is being presented, thus the error you are getting
当视图控制器需要更改视图时,使用委托,没有视图本身更改视图,这可能导致视图在呈现新视图时尝试解除分配,从而导致错误
#1
1
Use delegation when a view controller needs to change views, do not have the view itself change views, this could cause the view trying to deallocating while the new view is being presented, thus the error you are getting
当视图控制器需要更改视图时,使用委托,没有视图本身更改视图,这可能导致视图在呈现新视图时尝试解除分配,从而导致错误