Iterate All Sprite Nodes in SpriteKit using Swift [closed]

半腔热情 提交于 2020-01-26 02:30:13

问题


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


回答1:


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

self.enumerateChildNodesWithName(spriteName) {
            node, stop in
            // Do something with node.
}



回答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))
        }
    })


来源:https://stackoverflow.com/questions/26112340/iterate-all-sprite-nodes-in-spritekit-using-swift

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!