I looked through all posts, WWDC talks and youtube videos, but I still can't figure out how to have multiple SKscenes for different levels and connecting them with swift files in a Game using Spritekit. I want to have a main level board and I managed to open a new scene if you press a SKSpriteNode, but how can I implement game logic to this specific SKScene? Apologies in advance if it is a silly question, I've been spending ages on it.
Asked
Active
Viewed 2,743 times
4
-
Do you want to launch a new SKScene from the one you are currently in? – Steve Ives Mar 14 '16 at 22:57
1 Answers
7
You can do it like this:
1) Create .swift
file and make a subclass of a SKScene
Go to :
File menu -> New File -> Source -> Swift file
and make subclass of a SKScene
class MenuScene:SKScene {}
2) Create .sks file
Then go to
File menu -> New File -> Resource -> SpriteKit Scene
and make a MenuScene.sks
(name it MenuScene
without the actual extension).
Repeat this steps for each scene you want to have.
Then to load and start your initial scene. Do this inside your GameViewController.swift
:
if let scene = GameScene(fileNamed:"GameScene") {
let skView = self.view as! SKView
//setup your scene here
skView.presentScene(scene)
}
To make a transition to other scene (lets assume that you are in the MenuScene
currently) you should do something like this:
if let nextScene = GameScene(fileNamed: "GameScene"){
nextScene.scaleMode = self.scaleMode
let transition = SKTransition.fadeWithDuration(1)
view?.presentScene(nextScene, transition: transition)
}

Whirlwind
- 14,286
- 11
- 68
- 157
-
As an addition, read more here about having multiple scenes in a SpriteKit project. http://stackoverflow.com/a/35409586/3402095 – Whirlwind Mar 14 '16 at 23:27
-
Your post was very helpful I got it working now I also have read through the other post what you have linked that was also very helpful thank you very much for that good answer! – Ferdinand Lösch Mar 15 '16 at 14:32