问题描述
我正在使用 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 的成员.
代码如下:
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):
假设您为在子类中引入的任何新属性提供默认值,则适用以下两条规则:
规则 1 如果你的子类没有定义任何指定的初始化器,它自动继承其所有超类指定的初始化器.
Rule 1 If your subclass doesn’t define any designated initializers, itautomatically inherits all of its superclass designated initializers.
规则 2 如果您的子类提供了所有它的实现超类指定的初始化器——或者通过继承它们规则 1,或者通过提供自定义实现作为其一部分定义——然后它会自动继承所有的超类便利初始化器.
Rule 2 If your subclass provides an implementation of all of itssuperclass designated initializers—either by inheriting them as perrule 1, or by providing a custom implementation as part of itsdefinition—then it automatically inherits all of the superclassconvenience initializers.
这篇关于无法使用 unarchiveFromFile 在 SpriteKit 中设置 GameScene的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!