How to determine to which SpriteBatchNode the Sprites should be added

How to determine to which SpriteBatchNode the Sprites should be added
0.0 0


I’m trying to figure out how the Sprite sheets work. I found in a tutorial this code :

CCSpriteBatchNode *backgroundBgNode;
    backgroundBgNode = [CCSpriteBatchNode batchNodeWithFile:@"background.pvr.ccz"];
    [self addChild:backgroundBgNode];
    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"background.plist"];
    CCSprite *background = [CCSprite spriteWithSpriteFrameName:@"MuleDeer-ipadhd.png"];
    background.anchorPoint = ccp(0,0);
    [backgroundBgNode addChild:background];

Basically this code is clear but suppose that I have multiple spritesheets (pvr.czz files) and that every spritesheet has hundreds of different sprites. As suggested by the previous code I should add as a child every sprite to his corresponding CCSpriteBatchNode. Basically the problem is that the tool I use to generate spritesheets will almost randomly fit the sprites in different spritesheets if all the sprites do not fit in 1 spritesheet. What I have is a list of all the files (sprites) and I don’t know exactly to which spriteSheet they belong. So is there a way to get from code the right CCSpriteBatchNode to whom I should add my generating sprites ?


A simpler way to use them is to use CCSpriteFrameCache::addSpriteFramesFromFile()

// Load the spritesheet
CCSpriteFrameCache::sharedSpriteFrameCache() -> addSpriteFramesFromFile( "spritesheet.plist" );

// Create a sprite from a texture inside the spritesheet
CCSprite * mySprite = CCSprite::createWithSpriteFrameName( "Hero-Walk-0" );

// Add to layer
this -> addChild( mySprite );


I’m confused. Does this mean that I don’t have to load my textures (spritesheet. PVR.CZZ) with CCSpriteBatchNode and that the CCSpriteBatchnode should not be attached to the layer ? And how cocos2d knows about the existence of the pvc.czz file ?


When you use CCSpriteFrameCache::addSpriteFramesFromFile(), the textures inside the file you specified will automatically be added to the cache.


He is not asking about the cache, he is asking about SpriteBatchNode.

Claudio, you could try storing some map, where the key is a texture name and the value is a SpriteBatchNode. When you iterate over your sprites, you check its sprite sheet name by calling getTexture() and use it to search an appropriate SpriteBatchNode in the map.