This started out as a way to test and quickly roll through some animation I'm working on, and I thought it would be cool to share some of it with you. (I'm building on a thousand informative Cocos2d forum posts, and the animation tutorials by Ray Wenderlich and the rocking team at Get Set Games–so check out their blog posts on animation, awesome stuff).
One of things I love about the Cocos2D framework is how easy it is to set up a batch of animations and then shift between them with a call. So, you can load a spritesheet (an image with a bunch of images–sprites. These can be a sequence of frames in an animation–or more likely a bunch of sequences showing your characters, space ships, asteroids, frogs, sea dragons in different states–fighting, swimming, running, falling, etc.) and in the course of play and state change, your app shifts to different blocks of animation.
Here's a shot of a sea dragon animation I'm drawing–making it easy at this stage by doing everything on one side and mirroring it for the other:
There are certainly better ways to display animation in a real game, but for my test app I've set up a SeaDragonType, placing this in SeaDragonScene.h
DragonTypeLiving = 0,
And then I keep track of which dragon type is being displayed at the moment, looking for a screen tap to rotate through the dragon animations.
What dragon do I want to show now?
Run the action for that dragon type.
Stop the action for the prior dragon type.
_dragonType = DragonTypeDead;
I'm using Zwoptex (http://zwoptexapp.com) to create spritesheets. See Ray Wenderlich's post and the Zwoptex site for details on how to use the util. Powerful stuff. Here's my sheet for this app, three variations of the same sea dragon, a healthy living version, a sort of glowing one, and an undead version, each with the same bit of flipper action.
Using the Accelerometer on the iPhone and iPad to manipulate your sprites
This is easier than you might think. Let the app know you want to use the accelerometer up in your init:
// enable accelerometer
self.isAccelerometerEnabled = YES;
And then drop in the accelerometer callback, which let's us know what's going in with the device. (Debugging on the device is necessary for this, so set your target for Device and run to see this work). It doesn't do a lot, simply rotates the dragon–positioned in the center of the screen–in the direction the user tilts the iPhone/iPad.
static float prevX=0, prevY=0;
#define kFilterFactor 0.05f
float accelX = (float) acceleration.x * kFilterFactor +
float accelY = (float) acceleration.y * kFilterFactor +
prevX = accelX;
prevY = accelY;
_dragon.rotation = - (-accelX * 120);
Supporting different screen orientations in Cocos2d
I've basically swiped the UIKit rotation code Frogblast posted on the Cocos2D site, and included it in my test project to see what happens to the animation when I turn the device around. This method pushes the rotation handling off to a UIViewController (MultiplatformViewController) I'm using the UIInterfaceOrientationPortrait and UIInterfaceOrientationPortraitUpsideDown, and ignoring landscape views.
Here's the SeaDragonTest project zipped. I'm using Cocos2D v0.99.4 (current stable release) and iOS 4.1 SDK.
Have fun. Make something cool with Cocos2D!