Cocos2d游戏开发 3-Animations and Actions

本文详细介绍了使用Cocos2D框架创建游戏动画的步骤,包括创建动画帧、应用动画效果以及利用CCAnimationCache进行动画资源管理。同时,文章还探讨了如何在动画中实现状态机来模拟游戏角色的行为。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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];
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值