Game Development Reference
// sometime later:
CCAnimation* shipAnim = [shipSprite animationByName:shipAnimName];
The animationWithFile helper method makes two assumptions: animation image
filenames are consecutively numbered beginning with 0, and the files must have the
.png file extension. It's up to you whether to stick to this exact naming convention or
change it to accommodate your own needs. For example, you might find it more con-
venient to start numbering your animations starting with 1 instead of 0. In that case,
you'll have to change the for loop so that the name string is formatted with i + 1 .
The important part is to stick to whatever naming convention you choose to make your
life (and your code) easier.
You should take away three things from this:
▪ Encapsulate commonly used code by defining your own methods.
▪ Use Objective-C categories to add methods to existing classes.
▪ Define resource filenaming conventions to support your code.
Working with Texture Atlases
Texture atlases help conserve precious memory and speed up the rendering of sprites.
Because a texture atlas is nothing but a big texture, you can render all the images it
contains using a CCSpriteBatchNode , thus reducing the draw call overhead. Using
texture atlases is a win-win for both memory usage and performance.
What Is a Texture Atlas?
So far, for all the sprites used, I simply loaded the image file they need to display. In-
ternally, this image becomes the sprite's texture, which contains the image, but the tex-
ture width and height always have to be a power of two—for example, 1024×128 or
256×512. The texture size is increased automatically to conform to this rule, possibly
taking up more memory than the image size would suggest. For example, an image
with dimensions of 140×600 becomes a texture with dimensions of 256×1024 in
memory. This texture is wasting a lot of precious memory, and the amount of wasted