CCAnimation animation = CCAnimation.animationWithFrames(list);
[[CCSpriteFrameCache sharedSpriteFrameCache]addSpriteFramesWithFile:@"animationsFrames.plist"]; //左侧正常速度的播放 CCSprite*mySprite=[CCSprite spriteWithSpriteFrameName:@"himi1.png"]; mySprite.position=ccp(120,150); [self addChild:mySprite]; CCAnimation*anim=[CCAnimation animationWithFrame:@"himi" frameCount:12 delay:0.1]; CCAnimate* animate = [CCAnimate actionWithAnimation:anim]; CCSequence *seq = [CCSequence actions:animate,nil]; CCRepeatForever* repeat = [CCRepeatForever actionWithAction:seq]; [mySprite runAction:repeat];
CCTexture2D *texture = [[CCTextureCache sharedTextureCache] addImage:@"***.png"];
CCSpriteFrame *frame = [CCSpriteFrame frameWithTexture:texture rect:CGRectMake(0, 0, texture.contentSize.width, texture.contentSize.height)];
NSArray *array = [[NSArray alloc] initWithObjects:frame, nil];
CCAnimation *animation = [CCAnimation animationWithFrames:array delay:0.2f];
for(int i = 0; i < 3; i++)
{
int x = i % 3;
[animation addFrameWithTexture:mgr.texture rect:CGRectMake(x*32, 0, 31, 30)];
}
id action = [CCAnimate actionWithAnimation:animation];
[flight runAction:[CCRepeatForever actionWithAction:action]];
此处的fight是CCSprite类型的变量
附:c++风格代码
CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage(@"Animation\AndyBogard_by_SWP"); CCSpriteFrame fame0 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 0, 48 * 0, 32, 48)); CCSpriteFrame fame1 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 1, 48 * 0, 32, 48)); CCSpriteFrame fame2 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 2, 48 * 0, 32, 48)); CCSpriteFrame fame3 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 3, 48 * 0, 32, 48)); Listlist = new List (); list.Add(fame0); list.Add(fame1); list.Add(fame2); list.Add(fame3); CCAnimation animation = CCAnimation.animationWithFrames(list); CCSprite sprite = CCSprite.spriteWithSpriteFrame(fame0); sprite.position = new CCPoint(200, 200); addChild(sprite); CCAnimate animate = CCAnimate.actionWithAnimation(animation); animate.duration = 0.5f; sprite.runAction(CCRepeatForever.actionWithAction(animate));
基于cocos2d-x的动画都是采用了Action的方式来实现,这样做的导致了实现一个简单的动画就要写比较多的代码。在实际项目和开发中,可能需要我们自己去封装。
下面介绍一下,怎么在cocos2d-x for xna 中实现一个基本的动画。我准备一张动画源图,如下:
新建一个helloworld项目后,把图片加入到项目资源中。
在init()函数加入以下代码
#region 简单动画测试 CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage(@"Animation\AndyBogard_by_SWP"); CCSpriteFrame fame0 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 0, 48 * 0, 32, 48)); CCSpriteFrame fame1 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 1, 48 * 0, 32, 48)); CCSpriteFrame fame2 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 2, 48 * 0, 32, 48)); CCSpriteFrame fame3 = CCSpriteFrame.frameWithTexture(texture, new CCRect(32 * 3, 48 * 0, 32, 48)); Listlist = new List (); list.Add(fame0); list.Add(fame1); list.Add(fame2); list.Add(fame3); CCAnimation animation = CCAnimation.animationWithFrames(list); CCSprite sprite = CCSprite.spriteWithSpriteFrame(fame0); sprite.position = new CCPoint(200, 200); addChild(sprite); CCAnimate animate = CCAnimate.actionWithAnimation(animation); animate.duration = 0.5f; sprite.runAction(CCRepeatForever.actionWithAction(animate)); #endregion
编辑运行就可以见到效果了。
简单过程是,使用CCTexture2D加载图片 ,用CCTexture2D生成对应的CCSpriteFrame(对应的就是帧),将CCSpriteFrame添加到CCAnimation生成动画数据,用CCAnimation生成CCAnimate(就是最终的动画动作),最后用CCSprite执行这个动作。