Swift 多关卡场景 [英] Swift multiple level scenes

查看:27
本文介绍了Swift 多关卡场景的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试开发一个包含多个关卡的新游戏项目.我正在阅读这个问题(Sprite Kit - 定义多个场景的变量) 关于使用尽可能少的重复代码制作多个场景的最佳方法.答案当然是子类化.

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.

推荐答案

你可以在level1Scene中重写baseScene的函数,你只需要确保你调用了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 多关卡场景的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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