How can you connect a swift file with an SKscene?

我与影子孤独终老i 提交于 2019-12-05 04:30:00

问题


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.


回答1:


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)
}


来源:https://stackoverflow.com/questions/35998443/how-can-you-connect-a-swift-file-with-an-skscene

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!