我在GameCenter遇到2个问题。
如果玩家未登录GameCenter,则在提示您尚未登录的警告提示后,我将无法再单击视图上的任何按钮。
如果玩家登录GameCenter,则在访问排行榜后,按“完成”将不会退出排行榜视图。
这是与GameCenter相关的代码:
GameViewController:
func authPlayer() { // Gets called in ViewDidLoad
let localPlayer = GKLocalPlayer.localPlayer()
localPlayer.authenticateHandler = {
(view, error) in
if view != nil {
self.presentViewController(view!, animated: true, completion: nil)
}
else {
print(GKLocalPlayer.localPlayer().authenticated)
}
}
}
GameScene:
if gameCenterBtn.containsPoint(location) { // In touches began for touch in touches
saveHighScore(highLevel)
let viewController = self.view!.window?.rootViewController
let gcvc = GKGameCenterViewController()
viewController?.presentViewController(gcvc, animated: true, completion: nil)
}
func saveHighScore(number : Int) {
if GKLocalPlayer.localPlayer().authenticated {
let scoreReporter = GKScore(leaderboardIdentifier: "myLeaderBoardID")
scoreReporter.value = Int64(number)
let scoreArray : [GKScore] = [scoreReporter]
GKScore.reportScores(scoreArray, withCompletionHandler: nil)
}
}
func gameCenterViewControllerDidFinish(gameCenterViewController: GKGameCenterViewController) {
gameCenterViewController.dismissViewControllerAnimated(true, completion: nil)
}
任何和所有帮助将不胜感激
最佳答案
1)不确定这是否有帮助,但是您的登录代码不太正确。如果没有登录视图控制器,则不一定意味着播放器已登录。您也无法处理可选错误。试试这个吧。
localPlayer.authenticateHandler = { [unowned self] (viewController, error) in // will handle login changes also
if let error = error {
print(error.localizedDescription)
return
}
if let viewController = viewController {
self.presentViewController(viewController, animated: true, completion: nil)
}
else if self.localPlayer.authenticated {
print("Player authenticated")
}
else {
print("Player not authenticated")
}
}
我不确定您的gameViewCntroller为什么不会响应。您是否不只是在gameViewController中加载第一个SKScene。
您能否再用一些代码描述之后无法使用的内容。
2)由于您未设置委托,因此无法关闭屏幕。
您在创建游戏中心viewController的位置编写的代码应如下所示
let viewController = self.view?.window?.rootViewController
let gcvc = GKGameCenterViewController()
gcvc.gameCenterDelegate = self // YOU FORGOT THIS LINE
viewController?.presentViewController(gcvc, animated: true, completion: nil)