Search code examples
cocoaanimationsprite

sprite sheet animation in cocoa


I'm working on a simple iPad app which has an animation of 12 separate images using CAKeyFrameAnimation.

Just wondering what would be the simplest way to go with one sprite sheet and a plist file which describes that sprite instead of loading 12 separate images?


Solution

  • What I did was to load the spritesheet as a single image, load up the plist into bounds-normalized CGRects, then use a CAKeyframeAnimation to animate the layer's contentsRect on the @"sprite-animation" key.

    This gist contains the class I wrote. It doesn't have a method to load up a plist of animation names, frame indices, and timings, but it would be easy enough to write one. The main gotcha is that you must be sure to set the layer's frameSize or you won't get a working animation.

    One side-note: I wanted to animate my sprites' movement by hand, so I set the position key to a null animation action in -[SpriteLayer init]. You may want to remove that line.