I have a class 'GameplaySceneClass' which is an SKScene and I would like to pass parameters to it and then present the scene. The class can be seen below
class GameplaySceneClass: SKScene, SKPhysicsContactDelegate {
// var level: Float = the passed in level
override func didMove(to view: SKView) {
// print(level)
}
}
The presentation of the scene can be seen below within the 'GameViewController'.
if let view = self.view as! SKView? {
if let scene = GameplaySceneClass(fileNamed: "mainGameplayScene") {
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}
If I wanted to pass a value for 'level' to be used within the 'GameplaySceneClass', how would I do this?
I have tried using an initializer as can be seen below.
class GameplaySceneClass: SKScene, SKPhysicsContactDelegate {
var level: Float
init(level: Float) {
self.level = level
super.init()
}
required init?(coder: aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
override func didMove(to view: SKView) {
print(level)
}
}
However I doing so, I was unable to pass in the other paramater 'fileNamed'. How would I be able to do both?
I have managed to find an answer to my question, taken from here Creating a custom initalizer for SKScene that overrides convenience init?(fileNamed:)
The code becomes
class GameplaySceneClass: SKScene, SKPhysicsContactDelegate {
var level: Float?
override func didMove(to view: SKView) {
print(level)
}
}
if let view = self.view as! SKView? {
if let scene = GameplaySceneClass(fileNamed: "mainGameplayScene") {
scene.level = 1.0
scene.scaleMode = .aspectFill
view.presentScene(scene)
}
}