how to count number of sprites swift

陌路散爱 提交于 2019-12-08 06:39:44

问题


I'm building an app that add sprites to the screen. In several parts of my code I want to know how many sprites I have with a certain key. At the moment I implemented it in this way

var counter = 0
enumerateChildNodesWithName("box") { node, _ in
  counter = counter + 1
}
println(counter)

Is there another easier and shorter way? Thanks


回答1:


From iOS8, SKNode has subscript member that queries nodes and returns Array<SKNode>.

extension SKNode {
    subscript (name: String) -> [SKNode] { get }
}

So you can:

let count = self["box"].count
println(count)

instead of:

var counter = 0
self.enumerateChildNodesWithName("box") { _, _ in
    counter += 1
}
println(counter)


来源:https://stackoverflow.com/questions/27201797/how-to-count-number-of-sprites-swift

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