Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can you connect a swift file with an SKscene?

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.

like image 905
Ferdinand Lösch Avatar asked Mar 13 '23 22:03

Ferdinand Lösch


1 Answers

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)
}
like image 131
Whirlwind Avatar answered Apr 02 '23 20:04

Whirlwind