0

What is the best approach to iterate all SKSpriteNodes in a Scene using Swift?

Øyvind Vik
  • 5,867
  • 3
  • 14
  • 17
  • possible duplicate of [How to enumerate ALL nodes in a Sprite Kit scene?](http://stackoverflow.com/questions/25749576/how-to-enumerate-all-nodes-in-a-sprite-kit-scene) – Mike S Sep 30 '14 at 03:53

2 Answers2

2

If your sprites have their name properties set, you can use enumeration closure:

self.enumerateChildNodesWithName(spriteName) {
            node, stop in
            // Do something with node.
}
klaevv
  • 457
  • 1
  • 7
  • 17
2

I figured it out. Sample Swift code below.

self.enumerateChildNodesWithName("SomeSprite*", usingBlock: {
        (node: SKNode!, stop: UnsafeMutablePointer <ObjCBool>) -> Void in
        // do something with node or stop
            let action = SKAction.rotateByAngle(CGFloat(M_PI), duration:0.8)
            node.runAction(SKAction.repeatActionForever(action))
        }
    })
Øyvind Vik
  • 5,867
  • 3
  • 14
  • 17