Game Development Reference
In-Depth Information
@implementation CCAnimation (Helper)
// Creates an animation from single files
+(CCAnimation*) animationWithFile:(NSString*)name
frameCount:(int)frameCount
delay:(float)delay
{
// Load the animation frames as textures and create the sprite frames
NSMutableArray* frames = [NSMutableArray arrayWithCapacity:frameCount];
for (int i = 0; i < frameCount; i++)
{
// Assuming all animation files are named "nameX.png"
NSString* file = [NSString stringWithFormat:@"%@%i.png", name, i];
CCTexture2D* texture = [[CCTextureCache sharedTextureCache] addImage:file];
// Assuming that image file animations always use the whole image
CGSize texSize = texture.contentSize;
CGRect texRect = CGRectMake(0, 0, texSize.width, texSize.height);
CCSpriteFrame* frame = [CCSpriteFrame frameWithTexture:texture rect:texRect];
[frames addObject:frame];
}
// Return a CCAnimation object using all the sprite animation frames
return [CCAnimation animationWithSpriteFrames:frames delay:delay];
}
@end
Here's how the naming convention comes into play. The Ship 's animations have the
base name ship-anim followed by a consecutive number starting with 0 and ending
in the . png file extension. For example, the filenames for the Ship 's animation are
named ship-anim0.png through ship-anim4.png . If you create all your anim-
ations using that naming scheme, you can use the preceding CCAnimation extension
method for all your animations.
Tip I can't help but notice that a lot of developers and artists have a habit of
consecutively naming files with a fixed number of digits, by adding leading zer-
Search Nedrilad ::




Custom Search