R. Khaira R. Khaira - 11 days ago 6
Swift Question

Weird error with .presentScene / Swift 3 (SpriteKit)

I am getting a weird error with this code. So basically I am trying to transition scenes and I've typed the code and have looked over Apple's documentation multiple times. But cannot seem to figure this error out. Any help is appreciated. I am just trying to make a simple transition from Scene 1 to 2. I'm super irritated because I've searched so many places and cannot find the answer to this question. Thanks a ton.

As of right now, I have TWO scenes: The first one being "FirstScene" and the second one being "GameScene". I'm trying to transition from Scene 1(First Scene) to Scene 2(GameScene).


Error I'm getting: "Cannot convert value of type '(fileNamed: String)' to specified type 'SKScene!'"


let yourNextScene:SKScene! = (fileNamed: "GameScene")
let transition = SKTransition.moveIn(with: .right, duration: 1)
scene?.view?.presentScene(yourNextScene as SKScene, transition: transition)

Answer
let yourNextScene = SKScene(fileNamed: "GameScene")

Using : just specifies the type-- it doesn't automatically call the initializer .. This is basically just SKScene.init(fileNamed: "GameScene")

You could also do the verbose way:

let urnextscene: SKScene = SKScene(yaadayada)

Command click on SKScene and you will see the initializer in the SpriteKit.h file.

Comments