我在一个Case.sks (名称: SKSpriteNode )和一个标签(名称: SKLabel )中创建了一个场景Case.sks(使用关卡编辑器)。在我的主场景GameScene.sks中,我使用了一个带有“SKReferenceNode”的用例作为参考。
我需要从我的主场景访问“正方形”精灵。
我的第一个想法是直接调用子节点:
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如何获取参考场景的子节点?
发布于 2016-08-08 22:04:40
试着用下面的代码调用它:
override func sceneDidLoad() {
if let sprite = self.childNode(withName: "//square") as? SKSpriteNode {
// do whatever you want with the sprite
}
...
}https://stackoverflow.com/questions/38817793
复制相似问题