Game Development Reference
In-Depth Information
Shh, calm down—no need to take that Valium. If you pack your animation frames into
a texture atlas, things will get a bit easier and more efficient at the same time. More
helpful is to encapsulate all this code into a helper method and stick to a naming con-
vention for your animation files.
Animation Helper Category
Because the code to create the animation frames and the animation is common to all
animations, you should consider encapsulating this into a helper method. I've done so
by adding the CCAnimationHelper class to the project Sprite01. Instead of using
static methods, I extend the CCAnimation class using an Objective-C feature called
a category . Categories offer a way to add methods to an existing class without having
to modify the original class. The only downside is that with a category you can't add
member variables to the class; you can only add methods. The following code is the
@interface for the CCAnimation category, which I informally named Helper :
@interface CCAnimation (Helper)
+(CCAnimation*) animationWithFile:(NSString*)name
frameCount:(int)frameCount
delay:(float)delay;
@end
The @interface for an Objective-C category uses the same name as the class it ex-
tends and adds a category name within parentheses. The category name is like a vari-
able name and thus cannot contain spaces or other characters you can't use in vari-
ables—punctuation characters, for example. The @interface also must not contain
curly brackets, because adding member variables to a category isn't possible or al-
lowed.
The actual @implementation for the CCAnimation category uses the same
schema as the @interface by appending the category name in parentheses after the
class name. Everything else is just like writing regular class methods; in this case, my
extension method is named animationWithFile and takes the filename, the num-
ber of frames, and the animation delay as input:
Search Nedrilad ::




Custom Search