所以我试图将游戏中心添加到我的游戏中但是因为它在spriteKit中它不会工作..
我一直在寻找和寻找并且evrything指出它只能在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")
}
答案 0 :(得分:0)
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")
}