The Gibbinold
The Gibbinold

Reputation: 269

How to reload GameScene in SpriteKit

I have a simple game and when the time is up the game is over. I have a restart button which currently just reloads the GameScene but for some reason my Nodes appear halfway down and halfway to the left of the screen. I can still use the full space of the screen to play the game though.

The images below show the game while working, end game state and then after reloading the GameScene.

I'm not sure how to fix this or what code to provide.

This is my function for reloading the GameScene:

func goToGameScene(){
    let gameScene:GameScene = GameScene(size: self.view!.bounds.size) 
    let transition = SKTransition.fade(withDuration: 1.0)
    gameScene.scaleMode = SKSceneScaleMode.fill
    self.view!.presentScene(gameScene, transition: transition)
}

What the game should look like

End Game

After reloading the GameScene

Upvotes: 1

Views: 2509

Answers (1)

tommie de jong
tommie de jong

Reputation: 60

Replace:

gameScene.scaleMode = SKSceneScaleMode.fill    

With:

gameScene.scaleMode = .aspectFit

Or:

gameScene.scaleMode = .aspectFill  

You can also just completely copy the GameViewController code so you get something like this:

if let view = self.view {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "GameScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .aspectFit

                // Present the scene
                view.presentScene(scene)
            }

            view.ignoresSiblingOrder = true

            view.showsFPS = true
            view.showsNodeCount = true
        }

Just put that code where you want to reload the scene.

Upvotes: 3

Related Questions