我在一个SKSpriteNode(名称:正方形)和一个SKLabel(名称:label)中创建了一个场景Case.sks(使用“关卡编辑器”)。
在我的主要场景GameScene.sks中,我使用带有“Case”的SKReferenceNode作为引用。
我需要从主场景访问“方形” Sprite 。
我的第一个想法是直接调用子节点:
let firstSquare = childNode(withName: "square") as! SKSpriteNode
但是我得到了:
Fatal error: unexpectedly found nil while unwrapping an Optional value
所以我尝试了:
let caseRef = childNode(withName: "Case") as! SKReferenceNode
let firstSquare = caseRef.childNode(withName: "square") as! SKSpriteNode
但我上了firstSquare线:
Fatal error: unexpectedly found nil while unwrapping an Optional value
如何获得引用场景的子节点?
最佳答案
尝试使用以下代码进行调用:
override func sceneDidLoad() {
if let sprite = self.childNode(withName: "//square") as? SKSpriteNode {
// do whatever you want with the sprite
}
...
}
关于swift - 在SpriteKit SWIFT中获取SKReferenceNode的子节点,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/38817793/