Robert
Robert

Reputation: 38213

Cocos2D should I use one image per sprite

If I have several identical sprites on the screen at once, do I need to load one image for each of the sprite, or can I simply re-use the images somehow?

i.e,

Do I need to do this?

CCSprite *mySprite1 = [CCSprite spriteWithFile:@"mySprite.png"];
CCSprite *mySprite2 = [CCSprite spriteWithFile:@"mySprite.png"];

Or is there a better way to deal with identical sprites?

Upvotes: 2

Views: 390

Answers (2)

pyrosphere
pyrosphere

Reputation: 418

Your solution is fine. The first call to spriteWithFile: will load the texture and add it to the shared CCTextureCache. The second one will check to see if the texture already exists, and this time will reuse it from the first one.

Upvotes: 5

jsadfeew
jsadfeew

Reputation: 2297

You should propably use

- (id)initWithCGImage:(CGImageRef)image
                  key:(NSString *)key   

Reference

This method uses either image or a cached texture named by key. If no cached texture with this key is available, it'll create a texture and cache it for you.

Upvotes: 0

Related Questions