Casting an SKSpriteNode in an SKReferenceNode

188 views Asked by At

I've build a Scene1 in Xcode's Scene editor. And I've referenced another scene which has animation to this Scene1.

Now, I'm trying to cast-out an SKSpriteNode which is inside an SKReferenceNode. The name of the SKSpriteNode that I'm trying to cast, on a scene which was references is: "sc01eyelid".

Any suggestions what I might do wrong here?

thank you in advance.

import SpriteKit
import GameplayKit

class Scene1: SKScene {
    var misha: SKReferenceNode = SKReferenceNode()
    var eyelidForScene1:SKSpriteNode = SKSpriteNode()

override func didMove(to view: SKView) {        
    castMishaForScene1()
    castOutEyelid()
}

//Casting out misha
func castMishaForScene1() {
    if let someSpriteNode:SKReferenceNode = self.childNode(withName: "mishaRefNode") as? SKReferenceNode {
        misha = someSpriteNode
        print("CASTED\(misha)")
    }
    else {
        print("could not cast\(misha)")
    }
}

//Casting out eyelid
func castOutEyelid() {
    if let someSpriteNode:SKSpriteNode = misha.childNode(withName: "sc01eyelid") as? SKSpriteNode {
        eyelidForScene1 = someSpriteNode
        print("CASTED\(eyelidForScene1)")
    }
    else {
        print("could not cast\(eyelidForScene1)")
    }
}
}
1

There are 1 answers

0
Setera On

In order to access any Node of the SKRefference one needs to put additional "//" in the withName statement:

if let someSpriteNode:SKSpriteNode = misha.childNode(withName: "//sc01eyelid") as? SKSpriteNode {}

So withName: "//sc01eyelid" would work to access the sc01eyelid Node.

More info here: https://developer.apple.com/documentation/spritekit/sknode