6

I am just wondering what the simplest way to change scenes in Swift + Sprite Kit is?

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
user3808792
  • 103
  • 1
  • 2
  • 5
  • that would probably be reading the sprite kit programming guide - it's all in there. If you already know a way to change scenes and it's too complicated, then post that code. It's supposed to be a one liner with presentScene: in it. – CodeSmile Jan 13 '15 at 23:43

1 Answers1

12

when changing between scenes, you're going to need to set up a transition, how the scene will change to the next and define which scene you want to transition to.

For the transition,

 var transition:SKTransition = SKTransition.fadeWithDuration(1)

The fadeWithDurationcan be replaced with any SKTransition a list of which can be found in the documentation https://developer.apple.com/library/prerelease/ios/documentation/SpriteKit/Reference/SKTransition_Ref/index.html

as for defining the scene,

var scene:SKScene = GameScene(size: self.size)

You're stating which scene you wish to transition to, in this case GameScene, but should be replaced with whichever scene you wish to transition to.

In order to start the transition, call:

self.view?.presentScene(scene, transition: transition)

Which will move to the scene scene which was set up in the line prior, using the transition transition which was also defined.

Lazdo
  • 181
  • 1
  • 6
  • 1
    Minor update: Due to refactoring in later versions of Swift/SpriteKit, the fade method in the first line is now `SKTransition.fade(withDuration: 1)`. Other than that, this answer is simple, straightforward, and to the point. Thanks! – Kaji Apr 24 '20 at 01:26