如何将.sks文件添加到现有的Swift/Sprite-Kit项目中? [英] How to add an .sks files to existing Swift/Sprite-Kit project?

查看:137
本文介绍了如何将.sks文件添加到现有的Swift/Sprite-Kit项目中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我开始关注Ray Wenderlich的太空侵略者"教程,但分歧很大.现在,我有3个SKScenes-我的标题屏幕,主游戏屏幕和结束级别/游戏结束屏幕.我添加的标题屏幕和结束游戏场景都具有.sks文件;游戏主屏幕上没有,并且所有元素(SKSpriteNodes等)都以编程方式放置.我的程序流程如下:

I started following Ray Wenderlich's 'Space Invaders' tutorial, but have diverged considerably. I now have 3 SKScenes - my title screen, my main game screen and my end level/game over screen. The title screen and the end game scene I added and these both have .sks files; the main game screen does not and all elements (SKSpriteNodes etc) are placed programatically. The flow of my program is as follows:

我现在实际上想通过场景编辑器放置游戏主屏幕上的某些事件,因此我为其创建了一个.sks文件,并尝试如下更改我的titleScene.swift:

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:

来自:

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

收件人:

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

但是,这给出了:

我试图删除required init(coder aDecoder: NSCoder),但是Xcode抱怨

I tried to remove the required init(coder aDecoder: NSCoder) but Xcode then complains that

必需的init(coder:必须由SKScene的子类提供

required init(coder: must be supplied by subclass of SKScene

但是我的titleScenegameOverScene也是SKScene的子类,它们没有init(coder:)

However my titleScene and gameOverScene are also sub-classes of SKScene and they don't have init(coder:)

我真的看不到通过(fileNames :)和它们的.sks文件显示我的titleScreen和gameOverScene以及尝试对我的gameScene进行相同操作的区别.

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.

推荐答案

获得要求的原因是因为您拥有的变量不是可选的,或者在初始化发生之前没有初始化.

The reason why you are getting the required is because you have variables that are not optional or not initialized before init takes place.

如果您需要在init函数内部分配变量,则可以执行以下操作:

If you have variables that need to be assigned inside of an init function, then you would do:

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

但是然后您会问我:Knight0fDragon先生,它是告诉我用coder替换fileNamed,并且当我切换它时它没有编译.

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.

好吧,这是因为init(fileNamed:)是一个便利的初始化,而不是指定的初始化.为了能够继承一个类并获得所有便利的init,您需要重写所有指定的init.

Well this is because init(fileNamed:) is a convenience init, not a designated init. 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.

现在有了SKScene,您有3个,并且您已经知道1个.

Now with SKScene, you have 3, and you already know about 1.

让我们覆盖其他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.

好吧,我想做的是为在调用super之后必须在初始化的任何版本中分配的任何变量创建一个设置方法.

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之前,我们无法对常量执行此操作,因此我们需要在每个方法中进行设置.原因是self尚未完全存在.

Unfortunately we can't do this for constants before super is called, so those we would need to set up in each method. The reason being is that self does not fully exist yet.

最终看起来像这样:

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

这篇关于如何将.sks文件添加到现有的Swift/Sprite-Kit项目中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆