简体   繁体   English

如何将.sks文件添加到现有的Swift / Sprite-Kit项目?

[英]How to add an .sks files to existing Swift/Sprite-Kit project?

I started following Ray Wenderlich's 'Space Invaders' tutorial, but have diverged considerably. 我开始关注雷·温德利奇(Ray Wenderlich)的“太空侵略者”(Space Invaders)教程,但分歧很大。 I now have 3 SKScenes - my title screen, my main game screen and my end level/game over screen. 现在,我有3个SKScenes-我的标题屏幕,主游戏屏幕和结束级别/游戏结束屏幕。 The title screen and the end game scene I added and these both have .sks files; 我添加的标题屏幕和结束游戏场景都具有.sks文件; the main game screen does not and all elements (SKSpriteNodes etc) are placed programatically. 游戏主屏幕上没有,并且所有元素(SKSpriteNodes等)都以编程方式放置。 The flow of my program is as follows: 我的程序流程如下:

在此处输入图片说明

I now would actually like to place some events of the main game screen via the scene editor, so I created a .sks file for it and tried to change my titleScene.swift as follows: 我现在实际上想通过场景编辑器放置游戏主屏幕上的一些事件,因此我为其创建了一个.sks文件,并尝试如下更改我的titleScene.swift:

from: 从:

    let gameScene = GameScene(size:CGSize(width: 1536, height: 2048))

to: 至:

    let gameScene = SKScene(fileNamed: "GameScene.sks") as! GameScene!

However, this then gives: 但是,这给出了:

在此处输入图片说明

I tried to remove the required init(coder aDecoder: NSCoder) but Xcode then complains that 我试图删除required init(coder aDecoder: NSCoder)但是Xcode抱怨说

required init(coder: must be supplied by subclass of SKScene 必需的init(coder:必须由SKScene的子类提供

However my titleScene and gameOverScene are also sub-classes of SKScene and they don't have init(coder:) 但是我的titleScenegameOverScene也是gameOverScene的子类,它们没有init(coder :)

I really can't see the difference in what I'm doing to display my titleScreen and my gameOverScene via (fileNames:) and their .sks file and trying to do the same for my gameScene. 我真的看不到通过(fileNames :)和它们的.sks文件显示titleScreen和gameOverScene以及尝试对gameScene进行相同操作的区别。

The reason why you are getting the required is because you have variables that are not optional or not initialized before init takes place. 之所以需要此条件,是因为在初始化发生之前,您的变量不是可选的或未初始化的。

If you have variables that need to be assigned inside of an init function, then you would do: 如果您需要在init函数内部分配变量,则可以执行以下操作:

required init?(coder aDecoder: NSCoder)
{
   super.init(coder: aDecoder)
}

But then you will ask me: Mr Knight0fDragon, it is telling me to replace fileNamed with coder , and it is not compiling when I switch it. 但是然后您会问我:Knight0fDragon先生,它告诉我用coder替换fileNamed ,并且当我切换它时它没有编译。

Well this is because init(fileNamed:) is a convenience init, not a designated init. 好吧,这是因为init(fileNamed:)是方便的初始化,而不是指定的初始化。 In order to be able to subclass a class and get all of it's convenience inits, you need to override all of it's designated inits. 为了能够继承一个类并获得所有便利的init,您需要覆盖所有它指定的init。

Now with SKScene, you have 3, and you already know about 1. 现在有了SKScene,您就有3个,并且您已经知道1个。

Let's override the other 2: 让我们覆盖其他2:

override init() {
    super.init()
}
override init(size: CGSize) {
    super.init(size: size)
}

Alright, now this puppy should be ready to compile, we just need to get the variables assigned. 好了,现在这只小狗应该可以编译了,我们只需要获取分配的变量即可。

Well what I like to do is create a setup method for any variable that has to be assigned in any version of initialization after the super is called. 好吧,我想做的是为在调用super之后必须在初始化的任何版本中分配的任何变量创建一个设置方法。

Unfortunately we can't do this for constants before super is called, so those we would need to set up in each method. 不幸的是,在调用super之前我们无法对常量执行此操作,因此我们需要在每个方法中进行设置。 The reason being is that self does not fully exist yet. 原因是self还不完全存在。

This would end up looking like this: 最终看起来像这样:

let constant : String
required init?(coder aDecoder: NSCoder)
{
   constant = "hi"
   super.init(coder: aDecoder)
   setup()
} 
override init() {
    constant = "hi"
    super.init()
    setup()
}
override init(size: CGSize) {
    constant = "hi"
    super.init(size: size)
    setup()
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM