模板 ---------- 创建动画模板类

使用此类模板创建动画,帧动画

模板类文件

 1 AnimationUtil.h
 2 
 3 ///////////////////////////////////////////////////////////
 4 
 5 /*! TODO: long description:
 6  *  通过提供的动画图片的名字的前缀(如run)
 7  *  指定动画帧延迟,循环次数,即可生成一个Animation对象
 8  *  动画的命名规则:run1.png, run2.png, ...... , run16.png
 9  */
10 
11 //    filename    AnimationUtil.h
12 //    classname   AnimationUtil
13 //    author      silent
14 //    date        2015-12-28 23:18:08
15 //    version     1.0.0
16 //    Contact     user@company.com
17 
18 #ifndef __AnimationUtil_H__
19 #define __AnimationUtil_H__
20 
21 #include "cocos2d.h"
22 
23 USING_NS_CC;
24 
25 class AnimationUitl{
26 
27 public:
28 
29     // 根据文件名字前缀创建动画对象
30     static Animation *createWithSingleFrameName(const char *name, float delay, long iLoops);
31     
32     // 根据文件名字前缀创建动画对象,指定动画图片数量
33     static Animation *createWithFrameNameAndNum(const char *name, long iNum, float delay, long iLoops);
34 
35 protected:
36 
37 private:
38 
39 };
40 
41 
42 #endif // __AnimationUtil_H__
 1 AnimationUtil.cpp
 2 
 3 #include "AnimationUtil.h"
 4 
 5 Animation * AnimationUitl::createWithSingleFrameName(const char *name, float delay, long iLoops){
 6 
 7     SpriteFrameCache *cache = SpriteFrameCache::getInstance();
 8 
 9     Vector<SpriteFrame*> frameVec;
10     SpriteFrame *frame = NULL;
11     long index = 1;
12 
13     do 
14     {
15         frame = cache->getSpriteFrameByName(StringUtils::format("%s%d.png", name, index++));
16 
17         // 不断的获取SpriteFrame对象,知道获取的值为NULL
18         if (frame == NULL){
19             break;
20         }
21 
22         frameVec.pushBack(frame);
23 
24     } while (true);
25 
26     Animation *animation = Animation::createWithSpriteFrames(frameVec);
27     animation->setLoops(iLoops);
28     animation->setRestoreOriginalFrame(true);
29     animation->setDelayPerUnit(delay);
30 
31     return animation;
32 
33 }
34 
35 Animation * AnimationUitl::createWithFrameNameAndNum(const char *name, long iNum, float delay, long iLoops){
36 
37     SpriteFrameCache *cache = SpriteFrameCache::getInstance();
38 
39     Vector<SpriteFrame*> frameVec;
40     SpriteFrame* frame = NULL;
41     long index = 1;
42     while (index <= iNum){
43         frame = cache->getSpriteFrameByName(StringUtils::format("%s%d.png", name, index++));
44 
45         if (frame == NULL){
46             break;
47         }
48 
49         frameVec.pushBack(frame);
50         CCLOG("%d", index);
51     }
52 
53     Animation *animation = Animation::createWithSpriteFrames(frameVec);
54     animation->setLoops(iLoops);
55     animation->setRestoreOriginalFrame(true);
56     animation->setDelayPerUnit(delay);
57 
58     return animation;
59 }

 

使用方法:在函数中添加如下代码即可;

 1     // 测试AnimationUtil中的函数
 2     
 3     // 加载图片资源到缓存池中
 4     SpriteFrameCache *frameCache = SpriteFrameCache::getInstance();
 5     frameCache->addSpriteFramesWithFile("run.plist", "run.png");
 6 
 7     // 创建一个精灵
 8     Sprite *sprite1 = Sprite::createWithSpriteFrame(frameCache->getSpriteFrameByName("run1.png"));
 9     sprite1->setPosition(Vec2(200, 200));
10     this->addChild(sprite1);
11 
12     // 使用 AnimationUtil 工具创建动画
13     //Animation *animation1 = AnimationUitl::createWithSingleFrameName("run", 0.1f, -1);
14     Animation *animation1 = AnimationUitl::createWithFrameNameAndNum("run", 16, 0.1f, -1);
15 
16     // 使用精灵执行动画动作
17     sprite1->runAction(Animate::create(animation1));
18     

 

结果展示:

 

 

posted @ 2015-12-29 11:32  silent-bobo  阅读(240)  评论(0编辑  收藏  举报