简体   繁体   中英

Get child node of SKReferenceNode in SpriteKit SWIFT

I create a scene Case.sks (using Level Editor), inside one SKSpriteNode (name : square), and one SKLabel (name : label). In my main scene, GameScene.sks, I use a SKReferenceNode with "Case" for reference.

I need to access to the "square" sprite from my main scene.

My first idea was to call directly the child node:

 let firstSquare = childNode(withName: "square") as! SKSpriteNode

But I got :

 Fatal error: unexpectedly found nil while unwrapping an Optional value

So I tried :

 let caseRef = childNode(withName: "Case") as! SKReferenceNode
 let firstSquare = caseRef.childNode(withName: "square") as! SKSpriteNode

But I got on the firstSquare line :

 Fatal error: unexpectedly found nil while unwrapping an Optional value

How can get a child node of a reference scene ?

Try to call it with this code:

override func sceneDidLoad() {
     if let sprite = self.childNode(withName: "//square") as? SKSpriteNode {
           // do whatever you want with the sprite
     }
     ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM