一个精灵的动画该怎么理解?
我的理解就是场景中原本死气沉沉的精灵在原地动起来了。
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,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://www.jb51.cc/article/p-yugmmlhp-bch.html
http://www.jb51.cc/article/p-bwlbigwy-bch.html
http://cocos2d.9tech.cn/news/2014/0303/39946.html
http://www.jb51.cc/article/p-ffbjhgci-bme.html
总结以上是内存溢出为你收集整理的【cocos2dx之CCAnimation、CCAnimate、CCAnimationCache使用】全部内容,希望文章能够帮你解决【cocos2dx之CCAnimation、CCAnimate、CCAnimationCache使用】所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)