Search code examples
iossprite-kitswift2skscene

How to set the scalemode of a scene when I transit into it?


My problem is when I am trying to switch to a scene, the view of it is different when it was my start scene.

The problem is -I think- the scale mode, so I tried to set the scale mode but it didn't work. This what I have tried:

let skView = self.view! as SKView

let gameScene:GameScene = GameScene(size: self.frame.size)
gameScene.size = skView.bounds.size
gameScene.scaleMode = SKSceneScaleMode.ResizeFill

skView.presentScene(gameScene , transition: SKTransition.crossFadeWithDuration(1))

And this:

let gameScene = GameScene(size: self.size)

gameScene.scaleMode = SKSceneScaleMode.ResizeFill

self.view?.scene!.presentScene(gameScene , transition: SKTransition.crossFadeWithDuration(1))

And I have tried to set the scale mode of the scene in the didMoveToView() function.

None of these methods helps, any help?


Solution

  • Here's an example to transition from one scene to another. Supposed GameMenu is your first scene and you want to transition to GameScene.

    In GameViewController initialize GameMenu size:

    override func viewDidLoad() {
        super.viewDidLoad()
    
        let scene = GameMenu(size: view.bounds.size)
        // Configure the view.
        let skView = self.view as! SKView
        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 = .AspectFill
    
        skView.presentScene(scene)
    }
    

    Now, you should be at the first scene. When to transition to GameScene, add the following code as needed:

    let nextScene = GameScene(size: self.size)
    scene?.view?.presentScene(nextScene, transition: SKTransition.crossFadeWithDuration(1))
    

    And now everything's done and GameScene appears. Check this sample project if you have any problem.