gpt4 book ai didi

swift - 集成游戏中心 - 将类型 'UIView' 的值转换为 SKView”错误

转载 作者:行者123 更新时间:2023-11-30 13:28:20 25 4
gpt4 key购买 nike

所以我试图将游戏中心添加到我的游戏中,但由于它位于 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")
}

最佳答案

好吧,没关系,我找到了。

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

在 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")
}

关于swift - 集成游戏中心 - 将类型 'UIView' 的值转换为 SKView”错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36872419/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com