Create a new swift file and name it the same as your .sks file. Let's say you have a .sks file called MainMenu.sks
. You'd create a swift file called MainMenu.swift
. Inside that file, you'll want to create a Main Menu class that inherits from SKScene
.
import SpriteKit
class MainMenu: SKScene {
}
Inside there is where you'll put all your code. The key is, as you said, linking this to the .sks file.
When you go to present your scene, you'll instantiate the class and associate it with the .sks file.
import UIKit
import SpriteKit
class GameViewController: UIViewController {
override func viewDidLoad() {
super.viewDidLoad()
if let scene = MainMenu(fileNamed:"MainMenu") {
let skView = self.view as! SKView
// Some settings applied to the scene and view
// ... code ...
skView.presentScene(scene)
}
}
//... other code
}
Note the line let scene = MainMenu(fileNamed:"MainMenu")
. That's where you are instantiating the class you created in MainMenu.swift.
So, technically, MainMenu()
is the .swift
file and fileNamed: "MainMenu"
is the .sks
file. You can technically put any .sks
file into the init call and it'll render that scene.
Imagine having a game with all the logic for a maze runner. You could build all the game logic in a class called MazeScene
and just make a bunch of .sks
files, each with a different maze. You could then so something like, MazeScene(fileNamed: "MazeOne")
or MazeScene(fileNamed: "MazeThree")
.
For the documentation on this, SKScene inherits from SKNode so you'll find the documentation for init(fileNamed:)
here
That should get you going.