我正在使用 Xcode 7 beta 2 并按照 raywenderlich.com 的 Breakout 教程来学习 SpriteKit。这是当我尝试使用 unarchiveFromFile 加载 GameScene 时出现的错误。
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 开始可用。例如:
if let gameOverScene = GameOverScene(fileNamed: "GameOverScene") {
// ...
}
重要的是要注意init(fileNamed:)
是一个方便的初始化程序SKNode https://developer.apple.com/library/prerelease/mac/documentation/SpriteKit/Reference/SKNode_Ref/index.html#//apple_ref/occ/clm/SKNode/nodeWithFileNamed::
convenience init?(fileNamed filename: String)
因此,对于GameOverScene
自动继承init(fileNamed:)
, GameOverScene
必须遵守以下规则Swift 编程语言:初始化 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Initialization.html#//apple_ref/doc/uid/TP40014097-CH18-ID203(特别是规则 2):
假设您为在子类中引入的任何新属性提供默认值,则适用以下两条规则:
Rule 1如果你的子类没有定义任何指定的初始化器,它
自动继承其超类的所有指定初始值设定项。
Rule 2如果你的子类提供了它所有的实现
超类指定初始值设定项 - 通过按照以下方式继承它们
规则 1,或者通过提供自定义实现作为其一部分
定义——然后它自动继承所有超类
方便的初始化器。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)