swiftxcodesprite-kit

GameScene.swift and GameScene.sks not working together


I am unable to load my GameScene.sks file. I do not get any errors in either GameScene.swift or GameScene.sks

    import UIKit
    import SpriteKit

    class GameViewController: UIViewController {
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if let view = self.view as! SKView? {
            // Load the SKScene from 'GameScene.sks'
            if let scene = SKScene(fileNamed: "GameScene") {
                // Set the scale mode to scale to fit the window
                scene.scaleMode = .resizeFill
                
                // Present the scene
                view.presentScene(scene)
            }     
        }      
    }

Solution

  • I managed to fix the problem. Below is the working code.

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

    The problem was that instead of specifying what scene I was suppose to load I had used the more generic SKScene identifier

    if let scene = SKScene(fileNamed: "GameScene")
    

    The simple fix was changing the "SKScene" to my SKScene name.

    if let scene = GameScene(fileNamed: "GameScene") 
    

    Hope this may be of help to anyone else experiencing this issue.