oli
oli

Reputation: 136

How do I transition back to the GameScene in SpriteKit?

I have a SpriteKit game with two scenes. A GameScene and a 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.

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:

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:

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?'

I deleted the required init? from the GameScene and the error went away but then Xcode gave me another error unless I put the required init? back.

Previously I was able to run my game without the required init? but with the other code blocks.

Edit: Here is the GameScene constructor code.

//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)
    }

Upvotes: 0

Views: 647

Answers (1)

Sarah Pierson
Sarah Pierson

Reputation: 149

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:

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

Give it a try!

Upvotes: 1

Related Questions