简体   繁体   English

如何在SpriteKit中过渡回GameScene?

[英]How do I transition back to the GameScene in SpriteKit?

I have a SpriteKit game with two scenes. 我有一个带有两个场景的SpriteKit游戏。 A GameScene and a PlayScene. 一个GameScene和一个PlayScene。 I transition between the two scenes. 我在两个场景之间过渡。 Recently Xcode prompted me to add this code to my GameScene even though I didn't need it before when my game ran smoothly. 最近,Xcode提示我将此代码添加到我的GameScene中,即使在游戏平稳运行之前不需要它也是如此。

required init?(coder aDecoder: NSCoder) {
    fatalError("init(coder:) has not been implemented")
}

Xcode doesn't complain when I write this code to transition to the PlayScene from the GameScene: 当我编写以下代码以从GameScene过渡到PlayScene时,Xcode不会抱怨:

var scene = PlayScene(size: self.size)
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
scene.size = CGSizeMake(1536, 2048)
skView.presentScene(scene)

However when I use this almost identical code to transition back to the GameScene I get an error message: 但是,当我使用几乎相同的代码转换回GameScene时,出现错误消息:

var scene = GameScene(size: self.size)
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.scaleMode = .AspectFill
scene.size = CGSizeMake(1536, 2048)
skView.presentScene(scene)

error message: Cannot convert the expression's type '(size: @lvalue CGSize)' to type 'GameScene?' 错误消息:无法将表达式的类型'(size:@lvalue CGSize)转换为'GameScene?'

I deleted the required init? 我删除了required init? from the GameScene and the error went away but then Xcode gave me another error unless I put the required init? 从GameScene退出,错误消失了,但是Xcode给了我另一个错误,除非我放入required init? back. 背部。

Previously I was able to run my game without the required init? 以前我可以在没有required init?情况下运行我的游戏required init? but with the other code blocks. 但是还有其他代码块。

Edit: Here is the GameScene constructor code. 编辑:这是GameScene构造函数代码。

//called in viewDidLoad()
if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
        let skView = self.view as SKView
        scene.size = CGSizeMake(1536, 2048)
        skView.showsFPS = true
        skView.showsNodeCount = true
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .AspectFill
        skView.presentScene(scene)
    }

The funny thing is that I just had the same problem as you. 有趣的是,我和您有同样的问题。 It took me a while, but I solved it by adding this to GameScene: 我花了一段时间,但我通过将其添加到GameScene来解决了这个问题:

override init(size: CGSize) {
    super.init(size: size)
}

Give it a try! 试试看!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM