Curnelious
Curnelious

Reputation: 1

multiple CCSpritevFramevCache at one time

i am using the CCSpriteFrameCache a lot but cant understand somthing about it. can i load many .plist to the cache at the start of my game ? or the CCSpriteFrameCache has ONLY one plist at a time ?

by now there is a sprite which is a child of a CCSpriteBatchNode ,that is been created many times during the game , with different images. so every time i create a new sprite i do this:

    [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:[NSString stringWithFormat:@"cand%i.plist",stage]];
     candySheet = [CCSpriteBatchNode batchNodeWithFile:[NSString stringWithFormat:@"cand%i.png",stage]];
[self addChild:candySheet];
sprite1 = [CCSprite spriteWithSpriteFrameName:[NSString stringWithFormat:@"cand%i.png",1]];

is it ok ?

now lets say i have 2 spriteSheets, and 2 .plist, and i want to load both of them on my init , and add 2 sprites, each to be a child of one CCSpriteBatchNode and render images for each sprite from his own spriteSheet and Plist. but i get error then that :

CCSprite is not using the same texture id

so, i understand that each time i have to load to the cache the plist that i need at that specific time ????

thanks.

Upvotes: 0

Views: 366

Answers (2)

CodeSmile
CodeSmile

Reputation: 64478

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"some.plist"];

The above line takes quite a chunk of time even if the sprite frames are already loaded/cached. So make sure to avoid doing this unless you're absolutely certain that these sprite frames haven't been cached before, like at the beginning of the game or after purging the sprite frame cache.

Upvotes: 0

FBryant87
FBryant87

Reputation: 4595

Someone asked a similar thing earlier.

You cannot make sprites children of the same CCSpriteBatchNode if they are not from the same spritesheet.

You need to create a new CCSpriteBatchNode for each spritesheet you use (by spritesheet I mean the combined image file and .plist file)

The CCSpriteFrameCache is a single cache shared across all your scenes and classes. When you call this method:

[CCSpriteFrameCache sharedSpriteFrameCache]

You are not making a new CCSpriteFrameCache object everytime, there is just ONE instance. You store all your loaded spritesheets in this single cache. So you could load 2 spritesheets into the cache like so:

[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sheet1.plist"]; [[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"sheet2.plist"];

You then need to create a CCSpriteBatchNode for EACH spritesheet, you cannot have more than one sheet in a batch node:

CCSpriteBatchNode *spriteSheet1 = [CCSpriteBatchNode batchNodeWithFile:@"sheet1.pvr.ccz"];

CCSpriteBatchNode *spriteSheet2 = [CCSpriteBatchNode batchNodeWithFile:@"sheet2.pvr.ccz"];

You can then add both of these batch nodes to a layer if you wish. Sprites added to batch nodes must be from the spritesheet that batch node is using.

Upvotes: 1

Related Questions