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];
而 animationWithFrames的參數是一個NSArray類型的數據,所以要經過一些轉換后才能實現,具體實現參照如下例子:
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));
List<CCSpriteFrame> list = new List<CCSpriteFrame>();
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 for xna 學習筆記01:基本動畫實現
基於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));
List<CCSpriteFrame> list = new List<CCSpriteFrame>();
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執行這個動作。
