Eri-Sklii Eri-Sklii - 6 months ago 44
Swift Question

Changing scene causes zoom

When going from GameScene to PlayScene using this code below, all works fine:

for touch: AnyObject in touches {
let location = touch.locationInNode(self)
if self.nodeAtPoint(location) == self.playButton1 {
var scene = PlayScene(size: self.size)
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}
}


But when i am going from PlayScene to GameScene using this code below, it gets zoomed in:

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}


Tried to Updated code:

var scene = GameScene(size: self.size)
let skView = self.view as SKView!
skView.ignoresSiblingOrder = true
scene.scaleMode = .ResizeFill
scene.size = skView.bounds.size
skView.presentScene(scene)
}


How come? Any suggestions?

Answer

In the first code snippet, you create the new scene of self.size which keeps the same size as the old one. But in the second one, you use another method to do that, and the size of the new scene is initialized from the file GameScene.sks whose size is 1024x768 by default.


EDIT:

Did you use

if let scene = GameScene.unarchiveFromFile("GameScene") as? GameScene {...}

to create your first scene in your GameViewController.swift? If so, try to replace this line with

let scene = GameScene(size: view.bounds.size)

I'm afraid the reason why your buttons get resized(zoomed) is that they are not in the right size at the first time you load GameScene, actually they are in a scene sized 1024x768. Your button is hard-coded as 400 width which is nearly the 1/3 of the scene width (1024pt). When you load GameScene again, its width now becomes the screen width, so you find buttons get resized. Print scene.size.width for more info.

Comments