很简单的问题。我在 Objective C (cocos2d) 中工作,我正在尝试计算当前显示的图层上存在的某个类的 Sprite 的数量。例如,我有一个名为 Seal
的类这是 CCNode
的子类在我当前的图层中,我想计算类型 Seal
的实例数量存在。
我知道如何计算该层的 child 数
int numberChildren = [[self children] count];
正确返回层上子项的数量。但我只想要 Seal
的数量在我的图层上。我怎么能这样做?谢谢 =)
最佳答案
您可以使用谓词函数来做到这一点,例如:
NSArray * nodes = [self children];
NSIndexSet * sealSet = [nodes indexesOfObjectsPassingTest:^BOOL(id obj, NSUInteger idx, BOOL *stop)
{
return [obj isKindOfClass:[Seal class]];
}];
NSArray * sealArray = [nodes objectsAtIndexes:sealSet];
NSUInteger numberOfSeals = [sealArray count];
编辑: 其实你不必在一个新的数组中存储印章,你可以简单地计算它们:
NSUInteger numberOfSeals = [sealSet count];
关于ios - 计算某一层上某个类的 CCNodes 的数量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22738577/