如何在GameViewController.swift中加载与GameScene.swift不同的spritekit文件?

时间:2016-01-21 02:18:21

标签: xcode swift sprite-kit swift2 xcode7

在GameViewController.swift中,我找到了这一行

if let scene = GameScene(fileNamed:"GameScene") {

我一直认为,如果我想加载GameScene.swift以外的东西,我可以将“GameScene”更改为“LandingPage”,一切都可以正常工作。但是,正如我最近发现的那样,“GameScene”在这里实际上是指GameScene.sks而不是swift文件。

我正在寻找一个具有多个级别的游戏,每个级别都用自己的swift文件编写。我应该去哪里/如何从level1.swift移动到level2.swift?

3 个答案:

答案 0 :(得分:2)

如果您在一个场景中,例如关卡选择场景,可以说!,您可以通过@echo off setlocal disabledelayedexpansion rem Getting all the parameters passed to the program in the array 'params': rem Delayed expansion is left disabled in order not to interpret "!" in the program parameters' values (%1, %2, ... ); rem If a program parameter is not quoted, special characters in it (like "^", "&", "|") get interpreted at program launch. set /a count=1 :loop1 set "param=%~1" if defined param ( set "params_%count%=%param%" set /a count+=1 shift goto :loop1 ) set /a params_0=count-1 echo. rem After the array variables are set, delayed expansion can be enabled and "!" are not interpreted in the array variables's values: rem Printing the array 'params': echo Printing the elements of the array 'params': setlocal enabledelayedexpansion if defined params_0 ( for /l %%i in (1,1,!params_0!) do ( echo params_%%i="!params_%%i!" ) ) endlocal pause&echo. rem Setting the array 'filenames' with the list of filenames in the current directory: rem Delayed expansion is left disabled in order not to interpret "!" in the %%i variable's value; set /a count=0 for %%i in (*) do ( set "current_filename=%%~i" set /a count+=1 call set "filenames_%%count%%=%%current_filename%%" ) set /a filenames_0=count rem After the array variables are set, delayed expansion can be enabled and "!" are not interpreted in the array variables's values: rem Printing the array 'filenames': echo Printing the elements of the array 'filenames': setlocal enabledelayedexpansion if defined filenames_0 ( for /l %%i in (1,1,!filenames_0!) do ( echo filenames_%%i="!filenames_%%i!" ) ) endlocal endlocal pause goto :eof 的{​​{1}}方法转到另一个场景。

LevelSelectionScene.swift

答案 1 :(得分:1)

正如上面有人说的那样,行“fileNamed:”GameScene“”

 if let scene = GameScene(fileNamed:"GameScene") {

引用GameScene.sks文件,相当于游戏的故事板。

如果您创建一个新的SKScene类并尝试以这种方式加载它,无论是从另一个SKScene还是gameViewController它都无法工作,因为它无法找到相应的sks文件。

我不是100%确定如何创建新的sks文件,因为我在游戏中不使用它。

如果您只想加载自己创建的新场景,可以在gameViewController中使用此代码

let skView = self.view as! SKView
let scene = NewScene(size: skView.bounds.size)

skView.ignoresSiblingOrder = true
skView.multipleTouchEnabled = true
scene.scaleMode = .AspectFill
skView.presentScene(scene)

如果你想从SKScene类加载另一个场景,它基本上是相同的代码

let newScene = NewScene(size: self.size) //size of current scene
let  transition = SKTransition.doorsCloseHorizontalWithDuration(0.5) // use transition between 2 SKScenes
let skView = self.view as! SKView            
skView.ignoresSiblingOrder = true
skView.multipleTouchEnabled = true
newScene.scaleMode = .AspectFill
skView.presentScene(newScene, transition: transition)

或稍微清洁的版本

let newScene = NewScene(size: self.size)
let transition = SKTransition.doorsCloseHorizontalWithDuration(0.5)
newScene.scaleMode = SKSceneScaleMode.AspectFill
self.view?.presentScene(newScene, transition: transition)

答案 2 :(得分:0)

您可以在自定义类检查器中为每个场景文件指定自定义类名。这是“工具”窗格中最正确的选项卡。

加载sks文件时会加载自定义类(必须是SKScene的子类)。