Animations
1. All animations are just individual image frames being switched multiple times per second. This means your sprites have their display frames changed to different textures with a set delay between the time each texture is
displayed.
2. There are two steps to animate your sprites
(1)Create a CCAnimation to specify the set of CCSpriteFrame(CSpriteFrame object containing the location information) in your animation.
(2)Create a CCAnimate action, and “run” it on a sprite. A CCAnimate action specifies the CCAnimation to use and the delay to use between its frames.
CCSprite *animatingRobot = [CCSprite spriteWithFile:@"an1_anim1.png"];
[animatingRobot setPosition:ccp([vikingSprite position].x + 50.0f,
[vikingSprite position].y)];
[self addChild:animatingRobot];
CCAnimation *robotAnim = [CCAnimation animation];
[robotAnim addFrameWithFilename:@"an1_anim2.png"];
[robotAnim addFrameWithFilename:@"an1_anim3.png"];
[robotAnim addFrameWithFilename:@"an1_anim4.png"];
id robotAnimationAction =
[CCAnimate actionWithDuration:0.5f
animation:robotAnim
restoreOriginalFrame:YES];
id repeatRobotAnimation =
[CCRepeatForever actionWithAction:robotAnimationAction];
[animatingRobot runAction:repeatRobotAnimation];
// Get the sprite frame from the CCSpriteFrameCache
CCAnimation *exampleAnim = [CCAnimation animation];
[exampleAnim addFrame:[[CCSpriteFrameCache sharedSpriteFrameCache]
spriteFrameByName:@"sv_anim_2.png"]];
3. when you are creating animations for sprites using a texture atlas, the animation frames must come from the same texture atlas.
4. Each CCAnimation and CCAnimate action has two components: a delay between frames and a list of sprite frames to progress through. This kind of animation data is best stored in property list (plist) separate from your code.
5. In many games the AI is represented as state machines, with distinct operations at each state.
CCAnimationCache
1. Cocos2D comes bundled with a CCAnimationCache singleton that can cache all of your animations. Instead of storing the animations as instance variables, you can store them in the CCAnimationCache and
retrieve them as needed.
2. Three are two things you should be aware of
(1)When retrieving the animation, you should check that it is not nil. The CCAnimationCache can purge animations if asked via the purgeShared- AnimationCache call.
(2)Any animations you wish to keep around should be retained so that if they are purged from the cache, they will still be available to your objects.
[[CCAnimationCache sharedAnimationCache] addAnimation:animationToCache
name:@"AnimationName"];
CCAnimation *myAnimation = [[CCAnimationCache sharedAnimationCache]
animationByName:@"AnimationName"];
Actions
CCAnimate
Runs a particular animation with a set delay between each frame. This action is what you use to play an animation.
CCJumpBy
Simulates a parabolic jump automatically, given the height of the jump and the horizontal distance to cover.
CCRepeatForever
Repeats the action indefinitely. It has the effect of running an action on an infinite loop until stopAction: or stopAllActions is called.
CCSequence
Sequences two or more actions together so that after the first action completes, the second action is run.
CCSpawn
Fires off two or more actions simultaneously. CCSpawn is very useful for combining actions, such as a jump animation with the CCJumpBy action.
Using Property List to Store Animation Data


Bounding Box

1. In your game objects, this method adjusts the default sprite’s bounding box to com- pensate for the transparent space.
CCNode
-(CGRect)adjustedBoundingBox {
CCLOG(@"GameObect adjustedBoundingBox should be overriden");
return [self boundingBox];
}