Quicksillver Quicksillver - 25 days ago 11
Swift Question

Gamescene.sks and GameScene.swift don't seem to be linked in my project

I am learning SpriteKit and started a small project using the level editor.

However, the GameScene.swift and GameScene.sks don't seem to be linked for some reason. Below are the screenshots for the .sks file setup and a small print statement in viewDidLoad of the .swift file. Please let me know what I am doing wrong. Or if I am missing something.

SKS File.

The spritekit .sks file.

Swift File

Output

My View Controller's viewDidLoad function.

class GameViewController: UIViewController {

override func viewDidLoad() {
super.viewDidLoad()

let screenSize = CGSize(width: UIScreen.main.bounds.width, height: UIScreen.main.bounds.height)
GameController.gameController.setGameManager(gameViewManager: self);

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 = .aspectFill

// Present the scene
view.presentScene(scene)

}
}
}

Answer

We have a normal GameScene.swift that should be print "hello" with his GameScene.sks called by a GameViewController. You did not specify what is GameController but this is not a problem because I think you could simply lost the flow of your game so I try to help you about it (dont' worry, it happens when you are focused on the rest of the game a long time and can not remember the starting flow).

In your Main.storyboard you probably don't have a situation like this:

enter image description here

enter image description here

where your GameViewController is the initial view controller. What I mean is you should check who call GameViewController on your storyboard or if your AppDelegate.swift (in didFinishLaunchingWithOptions) stop the flow or call other views before calling GameViewController.

So , check who is the IVC (initial view controller) and be sure your GameViewController is called from start or by other viewControllers using breakpoint or simply launch this print in your viewDidLoad:

print("∙ \(type(of: self))")

Another situation could be happen when you add or remove files with the same name in library. In this case try to remove GameScene.swift and GameScene.sks, clean and build your project and re-add these files (be sure as your screen GameScene.sks have the right custom class..) Check your Compile Sources list to control your GameScene.swift

enter image description here

Hope it helps.