首页 > 代码库 > 【cocos2dx之CCAnimation、CCAnimate、CCAnimationCache使用】
【cocos2dx之CCAnimation、CCAnimate、CCAnimationCache使用】
一个精灵的动画该怎么理解?
我的理解就是场景中原本死气沉沉的精灵在原地动起来了。
CCAnimation和CCAnimate的官方源代码解释是下面这段话【版本cocos2dx-2.2.2】
/** A CCAnimation object is used to perform animations on the CCSprite objects. The CCAnimation object contains CCAnimationFrame objects, and a possible delay between the frames. You can animate a CCAnimation object by using the CCAnimate action. Example: [sprite runAction:[CCAnimate actionWithAnimation:animation]]; */ class CC_DLL CCAnimation : public CCObject { public: /** * @js ctor */ CCAnimation(); /** * @js NA * @lua NA */ ~CCAnimation(void); public: /** Creates an animation @since v0.99.5 */ static CCAnimation* create(void); /* Creates an animation with an array of CCSpriteFrame and a delay between frames in seconds. The frames will be added with one "delay unit". @since v0.99.5 @js create */ static CCAnimation* createWithSpriteFrames(CCArray* arrayOfSpriteFrameNames, float delay = 0.0f); /* Creates an animation with an array of CCAnimationFrame, the delay per units in seconds and and how many times it should be executed. @since v2.0 */ static CCAnimation* create(CCArray *arrayOfAnimationFrameNames, float delayPerUnit, unsigned int loops); static CCAnimation* create(CCArray *arrayOfAnimationFrameNames, float delayPerUnit) { return CCAnimation::create(arrayOfAnimationFrameNames, delayPerUnit, 1); } /** Adds a CCSpriteFrame to a CCAnimation. The frame will be added with one "delay unit". */ void addSpriteFrame(CCSpriteFrame *pFrame); /** Adds a frame with an image filename. Internally it will create a CCSpriteFrame and it will add it. The frame will be added with one "delay unit". Added to facilitate the migration from v0.8 to v0.9. * @js addSpriteFrameWithFile */ void addSpriteFrameWithFileName(const char *pszFileName); /** Adds a frame with a texture and a rect. Internally it will create a CCSpriteFrame and it will add it. The frame will be added with one "delay unit". Added to facilitate the migration from v0.8 to v0.9. */ void addSpriteFrameWithTexture(CCTexture2D* pobTexture, const CCRect& rect); /** * @lua NA */ bool init(); /** Initializes a CCAnimation with frames and a delay between frames @since v0.99.5 @lua NA */ bool initWithSpriteFrames(CCArray *pFrames, float delay = 0.0f); /** Initializes a CCAnimation with CCAnimationFrame @since v2.0 @lua NA */ bool initWithAnimationFrames(CCArray* arrayOfAnimationFrames, float delayPerUnit, unsigned int loops); /** * @js NA * @lua NA */ virtual CCObject* copyWithZone(CCZone* pZone); /** total Delay units of the CCAnimation. */ CC_SYNTHESIZE_READONLY(float, m_fTotalDelayUnits, TotalDelayUnits) /** Delay in seconds of the "delay unit" */ CC_SYNTHESIZE(float, m_fDelayPerUnit, DelayPerUnit) /** duration in seconds of the whole animation. It is the result of totalDelayUnits * delayPerUnit */ CC_PROPERTY_READONLY(float, m_fDuration, Duration) /** array of CCAnimationFrames */ CC_SYNTHESIZE_RETAIN(CCArray*, m_pFrames, Frames) /** whether or not it shall restore the original frame when the animation finishes */ CC_SYNTHESIZE(bool, m_bRestoreOriginalFrame, RestoreOriginalFrame) /** how many times the animation is going to loop. 0 means animation is not animated. 1, animation is executed one time, ... */ CC_SYNTHESIZE(unsigned int, m_uLoops, Loops) };
/** @brief Animates a sprite given the name of an Animation */ class CC_DLL CCAnimate : public CCActionInterval { public: /** * @js ctor */ CCAnimate(); /** * @js NA * @lua NA */ ~CCAnimate(); /** initializes the action with an Animation and will restore the original frame when the animation is over */ bool initWithAnimation(CCAnimation *pAnimation); /** * @js NA * @lua NA */ virtual CCObject* copyWithZone(CCZone* pZone); virtual void startWithTarget(CCNode *pTarget); virtual void stop(void); virtual void update(float t); virtual CCActionInterval* reverse(void); public: /** creates the action with an Animation and will restore the original frame when the animation is over */ static CCAnimate* create(CCAnimation *pAnimation); CC_SYNTHESIZE_RETAIN(CCAnimation*, m_pAnimation, Animation) protected: std::vector<float>* m_pSplitTimes; int m_nNextFrame; CCSpriteFrame* m_pOrigFrame; unsigned int m_uExecutedLoops; };
看着有点迷茫呀!!理一理思路。
其实我们在运用的过程中遵循以下步骤就行了。
一、创建CCAnimation
1.通过CCAnimationCache创建
CCAnimationCache *animationcache = CCAnimationCache::sharedAnimationCache(); animationcache->addAnimationsWithFile("animations/animations-2.plist"); CCAnimation *animation = animationcache->animationByName("dance_1");//刚开始不知道这个参数是哪里来的,其实是plist里面的,你用记事本打开就可以看得到,它是一个动画标识 animation->setRestoreOriginalFrame(true);2.通过CCAnimation的create***函数或是add***函数创建
用create***函数创建
CCArray *animations = CCArray::createWithCapacity(14); char str[100]={0}; for(int i = 1; i< 14; i++) { sprintf(str,”grossini_dance_%02d.png”,i); CCSpriteFrame *frame = frameCache->spriteFrameByName(str); animations->addObject(frame); } CCAnimation* animation = CCAnimation::createWithSpriteFrames(animations,2.0f);当然,你也可以通过add***函数创建
CCAnimation* animation = CCAnimation::create(); for( int i=1;i<15;i++) { char szName[100] = {0}; sprintf(szName, "Images/grossini_dance_%02d.png", i); animation->addSpriteFrameWithFileName(szName); //加载动画的帧 }二、通过CCAnimation创建CCAnimate
这个就比较简单了,一般就是
CCAnimate *animate = CCAnimate::create(animation);三、通过CCSprite的runaction绑定CCAnimate到精灵。
也比较简单
sprite->runAction(CCSequence::create(animate, animate->reverse(), NULL));
参考文章:
http://blog.csdn.net/ganpengjin1/article/details/17349353
http://blog.csdn.net/ganpengjin1/article/details/19121539
http://cocos2d.9tech.cn/news/2014/0303/39946.html
http://blog.csdn.net/hbhhww/article/details/13289319
【cocos2dx之CCAnimation、CCAnimate、CCAnimationCache使用】
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。