0
votes

I have a class representing my character in Cocos2d.
I've exported the spritesheet + .plist file.

The character has multiple animations.

The frames are simply called "Character_1.png".
Up to four. This is the basic walking animation.

In many sprite animation tutorials I found this sample code, which loops though the sprite frame cache and just adds it to an array so you can animate it:

for(int i = 1; i <= 4; ++i) {
    [animationFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:[NSString stringWithFormat:@"Character_%d.png", i]]];
}

However, because most of my sprites will be animated just like this, I'd like to delegate this to an super class.

I thought about calling it something like

spriteWithAnimations:(NSArray *)animationNames

and then I'd look though them, just like in the example above.

So as a parameter in the array I'd get a string @"Character_%d", I'd make a while loop and check if the file exists, and while so, I'd add it to the array.

The only problem is, that I cannot check if a frame "Character_05.png" exists, because CCSpriteFrameCache has no such method.

How is this usually solved?

1
check the CCAnimation extension class in Kobold2D www.kobold2d.com, it already does what you plan on doingLearnCocos2D
Sorry I have already defined in the concept that I'm using Cocos2d.IluTov

1 Answers

1
votes

If CCSpriteFrameCache returns nil for a given frame name you know it doesn't exist. Not elegant but it works.