gpt4 book ai didi

ios - GameOver 后在 GameViewController 和 MainViewController 之间使用 Swift、SpriteKit、Segue

转载 作者:行者123 更新时间:2023-11-30 13:26:27 24 4
gpt4 key购买 nike

我想在游戏结束后关闭 SKScene 并返回主菜单。

从MainViewController到GameViewController,我使用prepareForSegue制作了一个Storyboard Segue,以将一些数据从MainViewController传递到GameViewController

在 GameViewController 中,我做了这样的:

    override func viewDidLoad() {
super.viewDidLoad()

if let scene = GameScene(fileNamed:"GameScene") {
let skView = self.view as! SKView
skView.showsFPS = true
skView.showsNodeCount = true

skView.ignoresSiblingOrder = true

scene.scaleMode = .AspectFill

skView.presentScene(scene)

scene.viewController = self

}
}

然后,我在 GameScene.swift 中声明了 View Controller ,如下所示:

weak var viewController:GameViewController? = GameViewController()

我还在 GameScene.swift 中提供了以下函数来调用 endGameScene.swift:

    func endGame() {
// 1
gameOver = true

// 2
// Save high score
GameState.sharedInstance.saveState()

// 3
let reveal = SKTransition.fadeWithDuration(0.5)
let endGameScene = EndGameScene(size: self.size)
self.view!.presentScene(endGameScene, transition: reveal)

}

最后,endGameScene.swift调用dismissViewControllerAnimated,如下所示:

weak var viewController:GameViewController? = GameViewController()       

override func touchesBegan(touches: Set<UITouch>, withEvent event: UIEvent?) {
// Transition back to the Game
/*let reveal = SKTransition.fadeWithDuration(0.5)
let gameScene = GameScene(size: self.size)
self.view!.presentScene(gameScene, transition: reveal)*/

print(viewController)
self.viewController?.dismissViewControllerAnimated(true, completion: nil)

}

不幸的是, print(viewController) 返回 nil,当然,它不会关闭 ViewController。

我哪里出错了?

最佳答案

创建这样的函数

func goToGameScene(){
let gameScene:GameScene = GameScene(size: self.view!.bounds.size) // create your new scene
let transition = SKTransition.crossFadeWithDuration(1.5) // create type of transition (you can check in documentation for more transtions)
gameScene.scaleMode = SKSceneScaleMode.Fill
self.view!.presentScene(gameScene, transition: transition)
}

然后只需在您希望发生此转换的任何位置调用 goToGameScene() 即可。

您无需忽略任何内容,因为每次转换时它都会创建一个新场景。

关于ios - GameOver 后在 GameViewController 和 MainViewController 之间使用 Swift、SpriteKit、Segue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37126494/

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