Cannot use unarchiveFromFile to set GameScene in S

2019-03-03 19:11发布

问题:

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 does not have a member named unarchiveFromFile.

Here's the code:

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

回答1:

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

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

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

convenience init?(fileNamed filename: String)

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:

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

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.