无法使用 unarchiveFromFile 在 SpriteKit 中设置 GameScene [英] Cannot use unarchiveFromFile to set GameScene in SpriteKit

查看:15
本文介绍了无法使用 unarchiveFromFile 在 SpriteKit 中设置 GameScene的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Xcode 7 beta 2 并按照 raywenderlich.com 的 Breakout 教程学习 SpriteKit.这是我尝试使用 unarchiveFromFile 加载 GameScene 时遇到的错误.

I'm using Xcode 7 beta 2 and following raywenderlich.com's Breakout tutorial to learn SpriteKit. Here's the error I get when I try to load GameScene using unarchiveFromFile.

GameScene.type 没有名为 unarchiveFromFile 的成员.

GameScene.type does not have a member named unarchiveFromFile.

代码如下:

func didBeginContact(contact: SKPhysicsContact) {
    // 1. Create local variables for two physics bodies
    var firstBody: SKPhysicsBody
    var secondBody: SKPhysicsBody

    // 2. Assign the two physics bodies so that the one with the lower category is always stored in firstBody
    if contact.bodyA.categoryBitMask < contact.bodyB.categoryBitMask {
        firstBody = contact.bodyA
        secondBody = contact.bodyB
    } else {
        firstBody = contact.bodyB
        secondBody = contact.bodyA
    }

    // 3. react to the contact between ball and bottom
    if firstBody.categoryBitMask == BallCategory && secondBody.categoryBitMask == BottomCategory {
        //TODO: Replace the log statement with display of Game Over Scene

        if let mainView = view {
            let gameOverScene = GameOverScene.unarchiveFromFile("GameOverScene") as! GameOverScene
            gameOverScene.gameWon = false
            mainView.presentScene(gameOverScene)
        }
    }
}

推荐答案

您应该使用 init(fileNamed:) 初始化程序,它从 iOS 8 开始就可用.例如:

You should use the init(fileNamed:) initialiser, which is available from iOS 8 onwards. For example:

if let gameOverScene = GameOverScene(fileNamed: "GameOverScene") {
    // ...
}

请务必注意 init(fileNamed:)SKNode:

It's important to note that init(fileNamed:) is a convenience initialiser on SKNode:

convenience init?(fileNamed filename: String)

因此,GameOverScene 要自动继承 init(fileNamed:)GameOverScene 必须遵循 Swift 编程语言:初始化(尤其是规则 2):

Therefore, for GameOverScene to automatically inherit init(fileNamed:), GameOverScene must adhere to the following rules from The Swift Programming Language: Initialisation (rule 2 especially):

假设您为在子类中引入的任何新属性提供默认值,则适用以下两条规则:

Assuming that you provide default values for any new properties you introduce in a subclass, the following two rules apply:

规则 1 如果你的子类没有定义任何指定的初始化器,它自动继承其所有超类指定的初始化器.

Rule 1 If your subclass doesn’t define any designated initializers, it automatically inherits all of its superclass designated initializers.

规则 2 如果您的子类提供了所有它的实现超类指定的初始化器——或者通过继承它们规则 1,或者通过提供自定义实现作为其一部分定义——然后它会自动继承所有的超类便利初始化器.

Rule 2 If your subclass provides an implementation of all of its superclass designated initializers—either by inheriting them as per rule 1, or by providing a custom implementation as part of its definition—then it automatically inherits all of the superclass convenience initializers.

这篇关于无法使用 unarchiveFromFile 在 SpriteKit 中设置 GameScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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