Suppose I need several CCSprites using the same image. I can think of the 2 following solutions:
-
The image is in a separate file “bg.png”
CCSprite *image1 = [CCSprite spriteWithFile:@"bg.png"]; CCSprite *image2 = [CCSprite spriteWithFile:@"bg.png"]; -
The image is in a spritesheet “bg_sheet.png”
[[CCSpriteFrameCache sharedSpriteFrameCache] addSpriteFramesWithFile:@"bg_sheet.png"];Then
CCSprite *image1 = [CCSprite spriteWithSpriteFrameName:@"bg.png"]; CCSprite *image2 = [CCSprite spriteWithSpriteFrameName:@"bg.png"];
My questions are:
- I guess that in case 1 the image is loaded twice in memory, whereas in case 2 it’s loaded only once. Am I right ?
- Then does it mean that it’s ALWAYS better to use spritesheets?
- Did I miss any other better way to achieve it?
You are not right. In both cases image will be placed into the memory only once. You can check
spriteWithFile:code. It tries to find sprite frame in the sprite frame cache and load it only if there is no needed frame was found.Using spritesheets help to save memory. For example, for image with size 129×129 will be created texture with size 256×256. But you can add many of such images in one spritesheet and only one big texture will be created( i mean, it there will be spritesheet 1024×1024 or 2048×2048 there will be only one texture with the same size).