Welcome to my Programming Blog...

I am currently working on a couple of projects: An original game called "Implosion" which I am porting from Flash to the iPad, and a remake of Q*bert in Python (pygame), as part of the class I am teaching. Please feel free to use the "Labels" (at right) to follow a specific project or theme. If you are one of my Python students, I recommend that you check out the Python thread.

Tuesday, January 11, 2011

Cocos2d Animation quicksheet

Note: this is largely based on Steffen Itterheim's excellent code to make a helper file for the CCAnimation class. You can read more about it in his book.

(I've added two helper methods to his CCAnimationHelper class. For those of you in my class, I've included this updated version in the class handouts.)

//******************************
/**
 returns an action that will cause the animation to play for a sprite one time.
 */
-(CCAnimate*) singleAnimateAction:(CCAnimation*) animationFrames
{
   return [CCAnimate actionWithAnimation:animationFrames];
}


//******************************
/**
 returns an Action that will cause the animation to play for a sprite over and over.
 */
-(CCRepeatForever*) loopedAnimateAction:(CCAnimation*) animationFrames

   return [CCRepeatForever actionWithAction:[self singleAnimateAction:animationFrames]]; 
}

So here is the basic process:

Create a sequence of graphics files. Make all of them the same size, and name them with the same base name, with numbers (starting at zero) at the end. They all have to be .png files. For example: penguinFrame0.png, penguinFrame1.png, penguinFrame2.png.

If you are planning to use the frames as individual pictures....

  1. Drag the CCAnimationHelper files into your project's source folder.
  2. In your class's header file, #import "CCAnimationHelper.h." (Which header file? Whichever one will be using the rest of the code....)
  3. Import all of the pictures into the Resources section of your project.
  4. When you are ready to start animating, type in
    CCAnimation* theAnimation = [CCAnimation animationWithFile:@"penguinFrame" frameCount:10 delay: 0.15];
    (this assumes that you have 10 frames with a 0.15 second delay between each frame.)
  5. Then either say
    [mySprite runAction: [theAnimation singleAnimateAction]];
    or
    [mySprite runAction: [theAnimation loopedAnimateAction]];
If you are planning to use the frames with a SpriteBatchNode (much better and faster), you can put the frames from several different animations into one spritesheet.... It's basically the same process, except for step 4 is slightly different.
  1. Drag the CCAnimationHelper files into your project's source folder.
  2. In your class's header file, #import "CCAnimationHelper.h." (Which header file? Whichever one will be using the rest of the code....)
  3. Import all of the pictures into the Resources section of your project.
  4. When you are ready to start animating, type in
    CCAnimation* theAnimation = [CCAnimation animationWithFrame:@"penguinFrame" frameCount:10 delay: 0.15];
    (this assumes that you have 10 frames with a 0.15 second delay between each frame.)
  5. Then either say
    [mySprite runAction: [theAnimation singleAnimateAction]];
    or
    [mySprite runAction: [theAnimation loopedAnimateAction]];
That should do it! If you think you will use that animation frequently, you may want to store it in a variable, rather than reloading it each time...

No comments:

Post a Comment