Although it is possible to reuse the cocos2d v0.8 animation code, v0.99 introduces a new way to deal with animations (and sprites in general).
v0.99 has a new class named CCSpriteFrameCache that, as the name suggests, it is the cache of all the sprite frames.
You can load sprites frames to the CCSpriteFrameCache with:
- a plist filename (generated by Zwoptex editor)
- a plist filename (generated by Zwoptex editor) and a
CCTexture2D - a
CCSpriteFrameand a sprite frame name
( See the full API here: CCSpriteFrameCache API )
And the CCSpriteFrame object has the following properties:
- a
CCTexture2D: which texture should it use. - a
CGRect: the rectangle of the texture (from 1 texture you can create manyCCSpriteFrameobjects, in fact it is the recommend way to avoid wasting unnecessary memory).
Example:
// Obtain the shared instance of the cache CCSpriteFrameCache *cache = [CCSpriteFrameCache sharedSpriteFrameCache; // load the frames [cache addSpriteFramesWithFile:@"frames.plist"]; // It loads the frame named "frame1.png". // IMPORTANT: It doesn't load the image "frame1.png". "frama1.png" is a just the name of the frame CCSpriteFrame *frame = [cache spriteFrameByName:@"frame1.png"]; [sprite setDisplayFrame:frame];
This was just an introduction to CCSpriteFrameCache.
For further info, please read
- hhamm’s Programming iPhone Games with Cocos2D Part 3
- cocos2d programming guide: Animations





0 Responses to “Introduction to CCSpriteFrameCache”