无法使用 unarchiveFromFile 在 SpriteKit 中设置 GameScene
Cannot use unarchiveFromFile to set GameScene in SpriteKit
我正在使用 Xcode 7 beta 2 并按照 raywenderlich.com 的 Breakout 教程来学习 SpriteKit。这是我尝试使用 unarchiveFromFile 加载 GameScene 时遇到的错误。
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 开始可用。例如:
if let gameOverScene = GameOverScene(fileNamed: "GameOverScene") {
// ...
}
重要的是要注意 init(fileNamed:)
是 SKNode
上的便利初始化程序:
convenience init?(fileNamed filename: String)
因此,GameOverScene
要自动继承init(fileNamed:)
,GameOverScene
必须遵守The Swift Programming Language: Initialisation的以下规则(特别是规则 2):
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.
我正在使用 Xcode 7 beta 2 并按照 raywenderlich.com 的 Breakout 教程来学习 SpriteKit。这是我尝试使用 unarchiveFromFile 加载 GameScene 时遇到的错误。
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 开始可用。例如:
if let gameOverScene = GameOverScene(fileNamed: "GameOverScene") {
// ...
}
重要的是要注意 init(fileNamed:)
是 SKNode
上的便利初始化程序:
convenience init?(fileNamed filename: String)
因此,GameOverScene
要自动继承init(fileNamed:)
,GameOverScene
必须遵守The Swift Programming Language: Initialisation的以下规则(特别是规则 2):
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.