Game Development Reference
CCRepeatForever* repeat = [CCRepeatForever actionWithAction:animate];
At the very beginning of the code, I assigned the sharedSpriteFrameCache to a
local variable. The only reason to do so is that the [CCSpriteFrameCache
sharedSpriteFrameCache] singleton accessor is pretty lengthy to write.
To load a texture atlas, use the CCSpriteFrameCache 's method addS-
priteFramesWithFile and pass it the name of the .plist file for this texture
atlas. The CCSpriteFrameCache will load the sprite frames and will also try to
load the texture. Cocos2d automatically tries to load the -hd suffixed files on Retina
Note If you're using a large texture atlas texture—with dimensions of
1024×1024 or higher—you should load this texture before game play begins. It
will take a moment to load such a large texture (in the worst case it will freeze
the game for a few seconds). You may not notice the delay when testing on a
Retina iPad, but users with an iPhone 3GS will surely notice the delay.
Because the Ship class derives from CCSprite , and because I wanted it to use the
ship.png image from the texture atlas, I changed its initialization to use the
initWithSpriteFrameName method. This is identical to the code that initializes
a regular CCSprite from a texture atlas using a sprite frame name.
CCSprite* sprite = [CCSprite spriteWithSpriteFrameName:@"ship.png"];
If you load several texture atlases, and only one contains the sprite frame with the
name ship.png , cocos2d will still find that frame and use the correct texture for the
sprite. In essence, you work with the sprite frames by name as if they were the image's
filenames, but you don't need to know which texture contains the actual image (unless
you use a CCSpriteBatchNode , of course, which requires that all its children use
the same texture).
In Listing 6-13 you could get rid of most of the extra code required to initialize a CCS-
priteFrame object. There's no need any more to load a Texture2D and define the
texture's dimensions. Instead, simply call [CCSpriteFrame