Game Development Reference
os where necessary. For example, you might be tempted to name your files my-
anim0001 through my-anim0024 . I think this habit goes back to the good
ol' computer operating systems that were incapable of natural sorting and thus
incorrectly sorted filenames with consecutive numbers, with file1 followed by
file10 and then file2. Those days are long gone, and you'll actually make it
harder for the programmer to load files named like that in a for loop, because
you'll have to take into account how many leading zeros should be prepended.
There is a nice formatting shortcut, %03i , to prepend zeros so that the number
is always at least three digits long. However, I think it's better in our modern
world to just name filenames consecutively without prepending any leading
zeros. You gain a little bit of simplicity and peace of mind.
This greatly simplifies the code used to create an animation from individual files:
// The whole shebang is now encapsulated into a Category extension method
CCAnimation* anim = [CCAnimation animationWithFile:@"ship-anim"
Essentially this cuts down the number of lines from nine to just this one. As for the fi-
lename, you only need to pass the base name of your animation—in this case ship-
anim . The helper method adds the consecutive numbers based on the frameCount
parameter and also appends the .png file extension. You can also use the base name
for the animation as the name for the animation when you add it to the CCAnima-
tionCache so you don't have to remember alternate names for the same animation.
Previously I named the ship's animation move . Now it's called ship-anim , in line
with the filenames. You could store and access the animation from the CCAnima-
tionCache by using its base name like so:
NSString* shipAnimName = @"ship-anim";
CCAnimation* anim = [CCAnimation animationWithFile:shipAnimName
[[CCAnimationCache sharedAnimationCache] addAnimation:anim name:shipAnimName];