CCSpriteBatchNode 和 CCArray,查找非活动对象



对于一个简单的游戏,我有4个不同的平台(都在一个精灵表上)。我最初将每个 5 个添加到 CCSpriteBatchNode,并将它们全部设置为不可见。当我设置我的平台时,我想从我的CCSpriteBatchNode中获取某种类型的平台,并对其进行更改以使其可见并定位它。

我找不到不可见的特定类型的平台。反之亦然?

我知道你可以使用 [batchnode getchildbytag:tag],但据我所知,它只返回一个精灵。有什么方法可以将指向特定类型的每个平台的指针放入数组中,以便我可以遍历数组并找到所有不可见的精灵?

谢谢!

正如戏剧所建议的那样,你别无选择,只能"迭代"孩子们。至于识别哪个精灵对应哪个平台,有几种方法存在。一个简单的方法是使用精灵的"tag"属性 - 假设您不将其用于任何其他目的。

// some constants 
static int _tagForIcyPlatform = 101;
static int _tagForRedHotPlatform = 102;
... etc
// where you create the platforms
CCSptiteBatchNode *platforms= [CCSpriteBatchNode batchNodeWithFile:@"mapItems_playObjects.pvr.gz"];
CCSprite *sp = [CCSprite striteWithSpriteFrameName:@"platform_icy.png"];
sp.tag = _tagForIcyPlatform;
[platforms addChild:sp];
sp = [CCSprite striteWithSpriteFrameName:@"platform_redHot.png"];
sp.tag = _tagForRedNotPlatform;
[platforms addChild:sp];

// ... etc
// where you want to change properties of 
-(void) setVisibilityOf:(int) aPlatformTag to:(BOOL) aVisibility {
    for (CCNode *child in platforms.children) {
        if (child.tag != aPlatformTag) continue;
        child.visible = aVisibility;
    }
}

再一次,如果您不将平台子项的标签用于其他目的,这有效。如果需要标记用于其他目的,请考虑在类中使用 NSMutableArray(每个平台类型一个),并将指向相应类型的精灵的指针存储在该数组中。

没有一种超级简单的方法可以做到这一点。 您需要遍历子项并单独检查每个子项。

为了提高编码效率,请考虑向 CCSpriteBatchNode 添加一个类别来为您执行此功能。 这样,您可以根据需要轻松复制它。

最新更新