Game Development Reference
In-Depth Information
CCAnimation* anim = [CCAnimation animationWithSpriteFrames:frames delay:1];
// Store the animation in the CCAnimationCache
[[CCAnimationCache sharedAnimationCache] addAnimation:anim name:@"move"];
// Sometime later: retrieve the move animation from the CCAnimationCache
CCAnimation* move = [[CCAnimationCache sharedAnimationCache] animationByName:@"move"];
Going back to Listing 6-11 , notice the for loop. This is where it gets complicated. The
CCAnimation class must be initialized with an NSArray containing CCS-
priteFrame objects. A sprite frame consists only of a reference to a texture and a
rectangle that defines the area of the texture that should be drawn. The texture rect-
angle equals the texture's contentSize property—in other words, the size of the ac-
tual image contained in the texture. Keep in mind that the texture can be bigger than
the image it contains because textures can only have dimensions that are powers of
Now, unfortunately the CCSpriteFrame doesn't take an image filename as input; it
only accepts existing CCTexture2D objects. You create the texture using the
CCTextureCache singleton's addImage method, normally used to preload images
as textures into memory without having to create a CCSprite or other object. You
construct the filename using NSString 's stringWithFormat method, which al-
lows you to use the loop variable i to be appended to the filename, instead of having to
write out all five filenames.
To recap, from top to bottom, here's how you can create and run a sprite animation:
Create NSMutableArray .
For each animation frame:
a. Create a CCTexture2D for each image.
b. Create a CCSpriteFrame using the CCTexture2D .
c. Add each CCSpriteFrame to the NSMutableArray .
Create a CCAnimation using the frames in the NSMutableArray .
Optionally, add the CCAnimation to the CCAnimationCache with
a name.
Use a CCAnimate action to play the animation.
Search Nedrilad ::

Custom Search