My SKScene will show sized correctly in the Simulator, before rotation, but not after?

86 Views Asked by At

My SKScene will show sized correctly in the Simulator, before rotation, but not after?

If the Simulator is preset to Portrait, my SKScene is sized correctly .. and ditto if preset to Landscape.

But, not after rotation-in-place.

Here are some code snippets …

class GameViewController: UIViewController {

    override func viewDidAppear(_ animated: Bool) {
        
        super.viewDidAppear(animated)
        
        // thisScene = a String name, e.g. “CreditsScene” or “AboutScene”
        showScene(theScene: thisScene!)

    }   // viewDidAppear


    func showScene(theScene: String) {
                
        view = SKView()

        if let view = self.view as! SKView? {
            
            if let scene = SKScene(fileNamed: theScene) {
                scene.scaleMode = .aspectFill
                
                view.presentScene(scene)
            }
            
            view.ignoresSiblingOrder = true
            
            view.showsFPS = true
            view.showsNodeCount = true

        }

    }   // showScene


    override func didRotate(from fromInterfaceOrientation: UIInterfaceOrientation) {
        
        if fromInterfaceOrientation.isLandscape {
            let width =  UIScreen.main.bounds.width
            let height = UIScreen.main.bounds.height
            if let scene = SKScene(fileNamed: thisScene!) {
                print("\(scene)")
                scene.size = CGSize(width: width, height: height)
                print("\(scene)")
            }
        }
        else {
            let width =  UIScreen.main.bounds.width
            let height = UIScreen.main.bounds.height
            if let scene = SKScene(fileNamed: thisScene!) {
                print("\(scene)")
                scene.size = CGSize(width: width, height: height)
                print("\(scene)")
            }
        }
        
        showScene(theScene: thisScene!)
 
    }   // didRotate

}

Here’s the Console output after rotation from Portrait to Landscape:

name:'(null)' frame:{{-375, -667}, {750, 1334}} anchor:{0.5, 0.5} // before Landscape

name:'(null)' frame:{{-590, -410}, {1180, 820}} anchor:{0.5, 0.5} // after Landscape

Then, after rotation back to Portrait:

name:'(null)' frame:{{-375, -667}, {750, 1334}} anchor:{0.5, 0.5} // before Portrait

name:'(null)' frame:{{-410, -590}, {820, 1180}} anchor:{0.5, 0.5} // after Portrait

The 2nd lines, “after ..” reverse as they should.

For the life of me, I do not understand the 1st lines, “before”?

and for both lines what is name:'(null)' .. doesn't it have the name fileNamed:thisScene!

It’s almost as if

if let scene = SKScene(fileNamed: theScene) { .. }

doesn’t call view.presentScene( .. )

To complete the diagnostics, I have one GameViewController and 3 GameScenes (.sks files). I am successful in switching scenes .. but not in rotating them to size.

Any help is very, very appreciated!

1

There are 1 best solutions below

1
AudioBubble On
  1. See the above code for showScene, in particular the 1st line in the func showScene:

view = SKView()

Eliminate it.

  1. Found this jewel here at SO:

The size specified in the .sks file is the size of the scene not the view. Set your scene's scale mode to .resizeFill to force the scene size to always match your view size.

scene.scaleMode = .resizeFill