问题描述
我正在努力开发一个新的游戏项目,我将在其中包含多个级别。我正在阅读这个问题()关于使用尽可能少的重复代码进行多重场景的最佳方法。答案当然是子类化。
I am trying to work on a new game project where I will include multiple levels. I was reading this question (Sprite Kit - Defining the variables for multiple scenes) about the best way to do multiples scenes with as little duplicate code as possible. The answer of course is subclassing.
所以说我创建了我的baseScene,它是SKScene的子类。在这里,正如所建议的那样,我应该将所有相关代码(播放器,对象,碰撞位掩码,触摸开始函数等)放在所有子类级别场景中共享。
我在baseScene中使用通常的moveToView函数来添加内容,它在多个场景(level1Scene,level2Scene等)中都是完美的,它们都是baseScene的子类。同样适用于触摸开始功能等等,所以没有问题。
So say I create my "baseScene" which is a subclass of SKScene. Here, as suggested, I should put all the relevant code (player, objects, collisions bit masks, touches began functions etc) that will be shared across all subclass level scenes. I used the usual did moveToView function in baseScene to add the content and it works perfect across multiple scenes (level1Scene, level2Scene etc) that are all subclasses of baseScene. Same goes for touches began functions and so on, so no problem with that.
我现在的问题是,在我的level1Scene我不能为我的生活图如何添加在baseScene中的东西,如1级敌人,障碍物或背景。
我不能使用didMoveToView,因为它是一个覆盖函数,它将删除我在baseScene超类中添加的所有内容。
My issue now however is that in my "level1Scene" I cannot for the life of me figure out how to add stuff that's on top of what is in baseScene such as level 1 enemies, obstacles or backgrounds. I cannot use didMoveToView since it's an override function and will remove everything I have added in my baseScene superclass.
我将不胜感激任何支持,如果这是我的道歉是一个基本的,可能是愚蠢的问题,但我对swift相当新,特别是场景子类化。
I would appreciate any support and my apologises if this is a basic and probably stupid question but I am fairly new to swift, especially scene subclassing.
推荐答案
你可以覆盖 baseScene 在 level1Scene
中,您只需要确保调用 super
方法的版本。以下是一些示例,在 level1Scene
类中:
You can override functions of baseScene
in level1Scene
, you just need to make sure you call the super
version of the method. Here are a few examples, in your level1Scene
class:
override func didMoveToView(view: SKView) {
super.didMoveToView(view) // Calls `didMoveToView` of `baseScene`.
// Additional setup needed for `level1Scene`...
}
override func touchesBegan(touches: Set<NSObject>, withEvent event: UIEvent) {
super.touchesBegan(touches, withEvent: event) // Calls `touchesBegan` of `baseScene`.
// Additional stuff you want to do in `level1Scene`...
}
这篇关于Swift多级场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!