cocos2d中CCAnimation的使用(cocos2d 1.0以上版本)


在cocos2d  0.9及以下版本中,CCAnimation中可以使用animationWithName,但在1.0及以上版本中,此方法已經不能使用了,而是使用animationWithFrames替代,
 
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執行這個動作。




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM