gpt4 book ai didi

swift - 无法使用 unarchiveFromFile 在 SpriteKit 中设置 GameScene

转载 作者:行者123 更新时间:2023-11-28 09:59:28 24 4
gpt4 key购买 nike

我正在使用 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, itautomatically inherits all of its superclass designated initializers.

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.

关于swift - 无法使用 unarchiveFromFile 在 SpriteKit 中设置 GameScene,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31098884/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com