如何计算精灵数量 [英] how to count number of sprites swift
本文介绍了如何计算精灵数量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在构建一个将精灵添加到屏幕的应用程序.在代码的几个部分中,我想知道某个键有多少个精灵.目前,我以这种方式实现了
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
推荐答案
在iOS8 中, SKNode
具有 subscript
成员,用于查询节点并返回 Array< SKNode>
.
From iOS8, SKNode
has subscript
member that queries nodes and returns Array<SKNode>
.
extension SKNode {
subscript (name: String) -> [SKNode] { get }
}
因此您可以:
let count = self["box"].count
println(count)
代替:
var counter = 0
self.enumerateChildNodesWithName("box") { _, _ in
counter += 1
}
println(counter)
这篇关于如何计算精灵数量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文