Search code examples
iosswiftscreen-orientationdevice-orientation

SpriteKit GameScene Rotation Bug


I have been having a problem recently with this app which I encountered whilst trying to make the app universal and portrait orientation as well (the previous version was landscape on iPhone only). Now the app will open in portrait and look correct, but it is still in portrait when the device is rotated.

App Error

This is currently all the code I have for rotating the device (which works):

override func shouldAutorotate() -> Bool {
    return true
}

override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
    if UIDevice.currentDevice().userInterfaceIdiom == .Phone {
        return .AllButUpsideDown
    } else {
        return .All
    }
}

However, this only rotates the device and not the actual GameScene within which my game is contained. If the app is launched from a landscape homescreen, the game will stay in landscape.

For testing purposes, I can quit and relaunch the app from the simulator homescreen and the game will work perfectly again until the device is rotated.

How do I consistently present the GameScene in the correct orientation?


Solution

  • As Good Doug said try setting the skView size.

    In code you would load the first scene from the gameViewController like so. (This works if you dont use the scene sks files)

        let skView = view as! SKView!
        let scene = StartScene(size: skView.bounds.size)
    
        skView.ignoresSiblingOrder = true
        scene.scaleMode = .AspectFill
    
        skView.presentScene(scene)
    

    Also its seems like a very bad idea to make a game run in portrait and landscape, it will cause you so many problems I wouldn't even try.