Question

For a simple game, I have 4 different platforms (all on one spritesheet). I initially add 5 of each to a CCSpriteBatchNode, and set them all as not visible. When I set my platforms I want to take a platform of a certain type from my CCSpriteBatchNode and change it to make it visible and position it.

I am having trouble finding platforms of a specific type that aren't visible. Or vice-versa?

I know you can use [batchnode getchildbytag:tag] but as far as I know that only returns one sprite. Is there any way I can put pointers to each platform of a specific type into an array, so that I can iterate through the array and find all the not visible sprites?

Thanks!

Was it helpful?

Solution

As suggested by Drama, you will have no choice than to 'iterate' the children. As for identifying which sprite corresponds to which platform, a few ways exist. A simple one would be to use the 'tag' property of the sprite -- assuming you do not use it for any other purpose.

// 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;
    }
}

once again, this works if you are not using tags of platform's children for another purpose. If you need the tags for some other purpose, consider using an NSMutableArray in the class, one per platform type, and store in that array the pointer to your sprites of the appropriate type.

OTHER TIPS

There's not a super straightforward way to do that. You'll need to iterate through the children and inspect each child individually.

For coding efficiency, consider adding a category to CCSpriteBatchNode that performs this function for you. That way you can easily replicate it as needed.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top