I have the following boiler plate code generated by Xcode when creating a SpriteKit + GameplayKit project:
override func viewDidLoad() {
super.viewDidLoad()
// Load 'GameScene.sks' as a GKScene. This provides gameplay related content
// including entities and graphs.
if let scene = GKScene(fileNamed: "GameScene") {
// Get the SKScene from the loaded GKScene
if let sceneNode = scene.rootNode as! GameScene? {
// Copy gameplay related content over to the scene
// Set the scale mode to scale to fit the window
sceneNode.scaleMode = .aspectFill
// Present the scene
if let view = self.view as! SKView? {
view.presentScene(sceneNode)
view.ignoresSiblingOrder = true
view.showsFPS = true
view.showsNodeCount = true
}
}
}
}
For testing different scenes, I would prefer to move all code after super.viewDidLoad() into a function, so that I can easily test different scenes without having to edit a lot of code.
The line that I'm getting stuck on is:
if let sceneNode = scene.rootNode as! GameScene?
I am not sure how I can pass the class type in a generic manner to a function, so instead of GameScene?
, I can pass the correct scene type.
How do I do this ?