(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....
- Drag the CCAnimationHelper files into your project's source folder.
- In your class's header file, #import "CCAnimationHelper.h." (Which header file? Whichever one will be using the rest of the code....)
- Import all of the pictures into the Resources section of your project.
- 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.) - 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.
- Drag the CCAnimationHelper files into your project's source folder.
- In your class's header file, #import "CCAnimationHelper.h." (Which header file? Whichever one will be using the rest of the code....)
- Import all of the pictures into the Resources section of your project.
- 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.) - 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