开发者

how to implement two different animations on same sprite in cocos2d

I'm confused on using animations in cocos2d... I've a sprite which has 3 types of animations, like a smiley which laughs, cries and winks eyes... And I have separate sprite sheets for each of these animations... how will I be able to use these animations on the same sprite... Can anybody help me please???

Rega开发者_运维技巧rds,

Suraj


It would be much easier to have all your animations on the same sprite sheet, as, if your sprite is using a CCBatchnode to do it's draw method, you'd have remove it as a child from 1 sheet, and readd it to another.

In your CCSprite subclass, set some CCAction's as instance variables.

In an initialization method, write those actions and store them to the instance variables.

Then when you want to use an animation, tell your sprite to run it.

E.g.

NSMutableArray *smileFrames = [NSMutableArray array];
[smileFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"character_smile.png"]];
[smileFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"character_smile2.png"]];
[smileFrames addObject:[[CCSpriteFrameCache sharedSpriteFrameCache] spriteFrameByName:@"character_smile3.png"]];


CCAnimation *smileAnim = [CCAnimation 
                         animationWithFrames:smileFrames delay:0.2f];

self.smileAction = [CCSequence actions:
                   [CCAnimate actionWithAnimation:smileAnim restoreOriginalFrame:NO],
                   [CCCallFunc actionWithTarget:self selector:@selector(smileFinished)],
                   nil];

Then you would simply use..

[sprite runAction:smileAction];

I have added a CCCallFunc to the end of the animation, as you may want to revert back to an idle animation after it is finished.

Don't forget to release any retained actions when the sprite is deallocated.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜