1

I'm using Swift and Sprite Kit. I have an SKNode called MrNode that has several SKSpriteNodes and SKNode child nodes. Some of the SKNodes have children that have children of their own as well. To reference them, I do the following

var MrNode = SKNode?()
override func didMoveToView(view: SKView)
{
    MrNode = childNodeWithName("MrNode")
}

func DimLights()
{
    let liftmotor1:SKNode = MrNode!.childNodeWithName("LiftMotor1")!
    let liftPlatform1:SKSpriteNode = liftmotor1.childNodeWithName("LiftPlatform")as! SKSpriteNode
    let light1:SKSpriteNode = liftPlatform1.childNodeWithName("light1")as! SKSpriteNode
    let light2:SKSpriteNode = liftPlatform1.childNodeWithName("light2")as! SKSpriteNode
    light1.alpha = 0.2
    light1.alpha = 0.2
}

To find a SKSpriteNodes that is a child of child of a child of a child, I call DimLights(). Is this the best way to do this or is there something better?

0x141E
  • 12,613
  • 2
  • 41
  • 54
user2164327
  • 283
  • 1
  • 2
  • 13
  • 1
    This may be what you're looking for http://stackoverflow.com/questions/19709931/jumping-to-a-specific-sknode-in-node-hierarchy – 0x141E Aug 06 '15 at 19:31

2 Answers2

0

Yes, the intended way in Sprite Kit to access child nodes is through checking the name property of the children either through childNodeWithName() (the way you are doing) or by using a for loop to loop through the children array of a node.

You could alternatively declare variables directly inside of your class:

class exampleClass {
    var property: SKSpriteNode
}

This property would be visible to the entire class so you could access it from the class methods. However, if you have a lot of these then you'll notice the code quickly becomes messy.

MaxKargin
  • 1,560
  • 11
  • 13
0

If you need to frequently access light1 and light2 you can save a reference to them in a couple of weak properties.

I assumed mrNode, light1 and light2 will always be present so I declared the properties as implicitly unwrapped.

I also declared them weak because the related nodes will be keep alive by the reference for the parent node. However declaring them as week is not necessary.

As soon as possible (inside didMoveToView) I invoked populateProperties.

This method uses the optional binding technique to look for the nodes. If the IF fails then an error message is printed. However if the nodes are always present in the scene this will not happen.

Finally inside dimLights you can easily access your nodes using the related instance properties.

class MyScene : SKScene {

    weak var mrNode: SKNode!
    weak var light1: SKSpriteNode!
    weak var light2: SKSpriteNode!

    override func didMoveToView(view: SKView) {
        super.didMoveToView(view)
        populateProperties()
    }

    func dimLights() {
        light1.alpha = 0.2
        light2.alpha = 0.2
    }

    private func populateProperties() {
        if let
            mrNode = childNodeWithName("MrNode"),
            liftmotor1  = mrNode.childNodeWithName("LiftMotor1"),
            liftPlatform1 = liftmotor1.childNodeWithName("LiftPlatform"),
            light1 = liftPlatform1.childNodeWithName("light1") as? SKSpriteNode,
            light2 = liftPlatform1.childNodeWithName("light2") as? SKSpriteNode {
                self.mrNode = mrNode
                self.light1 = light1
                self.light2 = light2
        } else {
            debugPrintln("Error when populating properties")
        }
    }
}
Luca Angeletti
  • 58,465
  • 13
  • 121
  • 148