ios - 计算某一层上某个类的 CCNodes 的数量

标签 ios objective-c xcode cocos2d-iphone

很简单的问题。我在 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/

相关文章:

ios - swift 2.1 : CAGradientLayer color location & distribution

ios - 在 tableView 单元格大小上设置图像

iphone - 使用 NStimer 设置默认图像的图像动画

ios - 如何在没有 Storyboard的情况下实现 Size-Class-Specific Layout?

ios - 我们可以以编程方式设置显示选项卡栏时应显示哪个选项卡栏项目吗?

ios - UINavigationController 推送几个相同的viewController

ios - validateFunctionArguments :3379: failed assertion `Fragment Function , 纹理的像素格式(MTLPixelFormatRGBA16Unorm)

objective-c - 从NSString获取变量?

objective-c - 以编程方式显示相机快门?

c - 从 Xcode 在 OS X 本地安装编译代码