问题
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