如何将swift文件与SKscene连接?

时间:2023-01-22 23:40:49

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.

我查看了所有帖子,WWDC会谈和youtube视频,但我仍然无法弄清楚如何在不同级别使用多个SKscenes并使用Spritekit将它们与游戏中的swift文件连接起来。我想要一个主级别板,如果你按下SKSpriteNode,我设法打开一个新的场景,但是我如何为这个特定的SKScene实现游戏逻辑?如果这是一个愚蠢的问题,请提前道歉,我已经花了很多年的时间。

1 个解决方案

#1


6  

You can do it like this:

你可以这样做:

1) Create .swift file and make a subclass of a SKScene

1)创建.swift文件并创建SKScene的子类

Go to :

去 :

File menu -> New File -> Source -> Swift file

文件菜单 - >新建文件 - >源 - > Swift文件

and make subclass of a SKScene

并创建SKScene的子类

class MenuScene:SKScene {}

2) Create .sks file

2)创建.sks文件

Then go to

然后去

File menu -> New File -> Resource -> SpriteKit Scene

文件菜单 - >新文件 - >资源 - > SpriteKit场景

and make a MenuScene.sks (name it MenuScene without the actual extension).

并制作一个MenuScene.sks(将其命名为MenuScene,不带实际扩展名)。

Repeat this steps for each scene you want to have.

对要使用的每个场景重复此步骤。

Then to load and start your initial scene. Do this inside your GameViewController.swift:

然后加载并启动您的初始场景。在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:

要转换到其他场景(假设您当前在MenuScene中),您应该执行以下操作:

if let nextScene = GameScene(fileNamed: "GameScene"){
  nextScene.scaleMode = self.scaleMode                 
  let transition = SKTransition.fadeWithDuration(1)                  
  view?.presentScene(nextScene, transition: transition)
}

#1


6  

You can do it like this:

你可以这样做:

1) Create .swift file and make a subclass of a SKScene

1)创建.swift文件并创建SKScene的子类

Go to :

去 :

File menu -> New File -> Source -> Swift file

文件菜单 - >新建文件 - >源 - > Swift文件

and make subclass of a SKScene

并创建SKScene的子类

class MenuScene:SKScene {}

2) Create .sks file

2)创建.sks文件

Then go to

然后去

File menu -> New File -> Resource -> SpriteKit Scene

文件菜单 - >新文件 - >资源 - > SpriteKit场景

and make a MenuScene.sks (name it MenuScene without the actual extension).

并制作一个MenuScene.sks(将其命名为MenuScene,不带实际扩展名)。

Repeat this steps for each scene you want to have.

对要使用的每个场景重复此步骤。

Then to load and start your initial scene. Do this inside your GameViewController.swift:

然后加载并启动您的初始场景。在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:

要转换到其他场景(假设您当前在MenuScene中),您应该执行以下操作:

if let nextScene = GameScene(fileNamed: "GameScene"){
  nextScene.scaleMode = self.scaleMode                 
  let transition = SKTransition.fadeWithDuration(1)                  
  view?.presentScene(nextScene, transition: transition)
}