ios - 具有与多个子类共享的 BaseGame 类的单个 GKScene

标签 ios swift xcode sprite-kit skscene

我有一个 SpriteKit 场景文件、1 个 SKScene BaseGameClass,其中包含我的游戏的所有通用逻辑和 2 个用于不同游戏模式的 BaseGameClass 子类。

两个子类的游戏玩法基本相同,但规则/最终游戏状态等不同。

在我的主菜单场景中,我这样做:

switch node.name ?? "" {
    case "lblQuickGame":
        if let scene = GKScene(fileNamed: "GameScene") {
            // Get the SKScene from the loaded GKScene
            if let sceneNode = scene.rootNode as! QuickGameScene? {
                // Set the scale mode to scale to fit the window
                sceneNode.scaleMode = .aspectFill
                sceneNode.initializeGame(
                    gameSeed: .random(),
                    level: 1
                )
                self.view?.presentScene(sceneNode, transition: MenuScene.customSceneTransition())
            }
        }
...
...

我遇到的问题是游戏因这个错误而崩溃

Could not cast value of type 'SKScene' (0x107e0a1e0) to 'xxxx.QuickGameScene'

这是在提示我在哪里转换 scene.rootNode

是否可以有一个 SKS 文件,但允许它加载根节点的不同子类?!

<小时/>

更新:

SKS setting

这就是我的 SKS 文件的设置方式。我想避免每个 BaseGameScene 子类有 1 个 SKS,因为它们都是相同的,并且将成为需要维护的 PITA ;)

<小时/>

我想知道我的问题是否与继承有关(在本例中,我试图将父类(super class)命名为子类,这在面向对象中实际上无法做到)与某种抽象接口(interface)有关?

最佳答案

我想我已经明白了! MainMenu 场景不需要执行 GKScene 的操作,它可以直接使用适当的类加载 GameScene 文件并呈现该场景。另外,我必须从 SKS 文件中删除任何自定义类值。

func touchDown(atPoint pos : CGPoint) {
    let node = self.atPoint(pos)

    guard let view = self.view else {
        NSLog("No View Found")
        return
    }

    switch node.name ?? "" {
    case "lblTimeTrail":
        guard let scene = TimeTrailGameScene(fileNamed: "GameScene") else {
            NSLog("GameScene not Found")
            return
        }
        scene.scaleMode = .aspectFill
        scene.initializeGame(
            gameSeed: .random(),
            level: 1
        )

        view.presentScene(scene, transition: MenuScene.customSceneTransition())

    case "lblQuickGame":
        guard let scene = QuickGameScene(fileNamed: "GameScene") else {
            NSLog("GameScene not Found")
            return
        }
        scene.scaleMode = .aspectFill
        scene.initializeGame(
            gameSeed: .init(a: .cow, b: .snake, c: .goose),
            level: 1
        )

        view.presentScene(scene, transition: MenuScene.customSceneTransition())

    default:
            NSLog("Unknown node: %@", node)
    }
}

注意:我也许可以使这段代码更加优雅。让它工作,然后让它更快/更好;)

GKScene(fileNamed: ..., rootNode: ...) 路线是一条死胡同。这是一个完全没有记录的方法。

在这样的时刻,我怀念使用像 Cocos2D 这样的开源框架……至少这样我可以深入研究代码并理解某些东西是如何工作的;使用 SpriteKit,我们只得到方法头,而我们通常只能猜测它是如何工作的!

关于ios - 具有与多个子类共享的 BaseGame 类的单个 GKScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47817007/

相关文章:

iOS 内存管理/持久性问题

swift - 在 OSX 应用程序上运行测试时为 "Message from debugger: unable to attach"

ios - 识别标签中显示的属性字符串中的图像点击

objective-c - 如何在 Objective C 中获取这些对象数组中的对象的属性?

ios - 没有设置捆绑 IOS 的设置条目

Swift 类型下标

swift - 如何创建集合扩展来查找具有属性的集合中的唯一对象?

ios -/Applications/Xcode-beta.app/Contents/Developer/usr/bin/ibtool 失败,退出代码 255,Interface Builder

ios - 一旦我在STPAddCardViewController()上选择完成,应用程序就会加载并且不会停止加载

ios - 使用 NSPredicate 的方法减少代码重复