代码改变世界

cocos2dx 3.x(实现帧动画(人物动画,跑马灯效果)的几种方法)

2016-10-26 00:39  罗任德  阅读(5809)  评论(0编辑  收藏  举报
 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