cocos2dx 3.x(實現幀動畫(人物動畫,跑馬燈效果)的幾種方法)


 1 //創建一個跑酷的精靈 2  auto sprite = Sprite::create("1.png");

 3  //設置精靈的坐標 4  sprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));

 5 //添加到當前層 6  this->addChild(sprite);

 7 //創建序列幀動畫 8  auto animation = Animation::create();

 9 //設置動畫名字數組的長度10 char nameSize[20] = {0};

11  //動畫的循環 12張圖片12  for (int i =1; i<13; i++)

13  {

14        //循環遍歷15         sprintf(nameSize, "%d.png",i);

16          //添加到序列幀動畫17      animation->addSpriteFrameWithFile(nameSize);

18  }

19 //設置動畫幀的時間間隔20 animation->setDelayPerUnit(0.02f);

21 //設置播放循環 一直播放 為-122 animation->setLoops(-1);

23 //設置動畫結束后恢復到第一幀24 animation->setRestoreOriginalFrame(true);

25  //創建動畫動作26 auto animate = Animate::create(animation);

27  //播放動畫28 sprite->runAction(animate);

 

 

 1 //幀動畫緩存 2 auto frameCache = SpriteFrameCache::getInstance();

 3  frameCache02->addSpriteFramesWithFile("1.plist");

 4  //創建一個顯示動畫的精靈 5 auto sprite = Sprite::createWithSpriteFrameName("1.png");

 6 //設置動畫的坐標 7 sprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));

 8 //添加到當前層 9 this->addChild(sprite);

10 //
創建一個容器
11  Vector<SpriteFrame*> vec;

12  //設置動畫名字數組的長度13 char name[20] = {0};

14  for (int i = 1; i<13; i++) {

15 //遍歷16 sprintf(name, "%d.png",i);

17 vec.pushBack(frameCache->getSpriteFrameByName(name));

18  }

19  //auto animation = Animation::createWithSpriteFrames(vec,0.05f);

20  //也是可以這么寫的。那setDelayPerUnit 這個需要注釋掉21 auto animation = Animation::createWithSpriteFrames(vec);

22 //設置動畫幀的時間間隔23 animation->setDelayPerUnit(0.05f);

24  //設置播放循環 一直播放 為-125  animation->setLoops(-1);

26 //設置動畫結束后恢復到第一幀27 animation->setRestoreOriginalFrame(true);

28  //創建動畫動作29  auto animate = Animate::create(animation);

30  //播放動畫動作31 sprite->runAction(animate);

 

 

 1 //通過一張集合的圖片來創建
 2     //創建2D紋理
 3     auto texture = Director::getInstance()->getTextureCache()->addImage("dragon_animation.png");
 4     //建立圖片幀
 5     auto frame0 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*0, 132, 132));
 6     auto frame1 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*0, 132, 132));
 7     auto frame2 = SpriteFrame::createWithTexture(texture, Rect(132*2, 132*0, 132, 132));
 8     auto frame3 = SpriteFrame::createWithTexture(texture, Rect(132*3, 132*0, 132, 132));
 9     auto frame4 = SpriteFrame::createWithTexture(texture, Rect(132*0, 132*1, 132, 132));
10     auto frame5 = SpriteFrame::createWithTexture(texture, Rect(132*1, 132*1, 132, 132));
11     
12     auto sprite2 = Sprite::createWithSpriteFrame(frame0);
13     sprite2->setPosition(Vec2(visibleSize.width/2, visibleSize.height/2));
14     this->addChild(sprite2);
15     
16     //保存圖片幀
17     //Vector<cocos2d::AnimationFrame *> array;
18     Vector<cocos2d::SpriteFrame *> array;
19     array.pushBack(frame0);
20     array.pushBack(frame1);
21     array.pushBack(frame2);
22     array.pushBack(frame3);
23     array.pushBack(frame4);
24     array.pushBack(frame5);
25     
26     auto animation2 = Animation::createWithSpriteFrames(array, 0.2f);   //此處createWithSpriteFrames()函數確實每幀間隔時間參數,需自行加上去!!!
27     sprite2->runAction(RepeatForever::create(Animate::create(animation2)));
28     
29     

 


免責聲明!

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



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