Cocos2d-x學習筆記之動畫的處理

第一步解析plist文件,這個可以調用函數實現,加載爲動畫緩存(animationCache

 

#include"core/boy.h" #include "cocos2d.h" #include "core/Singleton.h"  using namespace cocos2d;  struct  ObjectAnimation {     char *animateName;//動畫名:對應文件開頭     int frameNum;//幀數     int starFrame; };  extern ObjectAnimation comboAnimation[6];  class CCParsePlistAnimation:public Singleton<CCParsePlistAnimation> { public:     bool loadAnimation(ObjectAnimation *af,int count);     cocos2d::CCAnimate* getAnimate(char *name);     CCAnimation* getAnimation(char* name); private:     char *getAnimationName(char *name); };  #define sCCParsePlistAnimation CCParsePlistAnimation::getInstance()   #endif//  #include "Common\CCParsePlistAnimation.h"//120 #include"DBgame.h" using namespace cocos2d;  static char charBuffer[128];  //af[1].animateName is plist ObjectAnimation comboAnimation[6]= {     {"gameObject",0,0},     {"combo",3,0},     {"coin",8,0},     {"cube",8,0},     {"critical_text",5,0},     {"box",6,0}, };  bool CCParsePlistAnimation::loadAnimation(ObjectAnimation *af,int count) {     //緩衝&mdash;&mdash;這會加載對應的png,並裁切成SpriteFrame,而且還會完成索引     memset(charBuffer,0,sizeof(charBuffer));     sprintf(charBuffer,"objectTexture/16bit/4444-%s.plist",af[0].animateName);      printf("OBJECTANIMATION:------%s\n",charBuffer);     CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile(charBuffer);     //創建動畫數據     CCMutableArray<CCSpriteFrame*> *spriteFrames = new CCMutableArray<CCSpriteFrame*>();     for (int i=1;i<count;i++)     {         for(int j=af[i].starFrame;j<af[i].starFrame+af[i].frameNum;j++)         {             memset(charBuffer,0,sizeof(charBuffer));             sprintf(charBuffer,"%s_%d.png",af[i].animateName,j);                          printf("objectPicture:--------%s\n",charBuffer);               CCSpriteFrame *spriteFrame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(charBuffer);             spriteFrames->addObject(spriteFrame);          }         //使用cache緩衝管理         CCAnimation *animation=CCAnimation::animationWithFrames(spriteFrames,0.2f);         memset(charBuffer,0,sizeof(charBuffer));          sprintf(charBuffer,"%s",af[i].animateName);          printf("objAnimation:%s\n",charBuffer);          CCAnimationCache::sharedAnimationCache()->addAnimation(animation,charBuffer);         spriteFrames->removeAllObjects();     }     spriteFrames->release();      return true; } cocos2d::CCAnimate* CCParsePlistAnimation::getAnimate(char *name) {     CCAnimation* animation=CCAnimationCache::sharedAnimationCache()->animationByName(getAnimationName(name));          if(animation)     {         return cocos2d::CCAnimate::actionWithAnimation(animation);     }     return NULL; } CCAnimation* CCParsePlistAnimation::getAnimation(char* name) {     CCAnimation* animation=CCAnimationCache::sharedAnimationCache()->animationByName(getAnimationName(name));      if(animation)     {         return animation;     }     return NULL;  } char* CCParsePlistAnimation::getAnimationName(char *name ) {     memset(charBuffer,0,sizeof(charBuffer));     sprintf(charBuffer,"%s",name);     return charBuffer; }

接下來進行初始化在HelloWorldSceneinit函數中初始化.

 

sCCParsePlistAnimation->loadAnimation(comboAnimation,6);

下面的部分就是動畫與sprite的結合的一些實用了,以及或許會遇到的動畫播放的細節的舉列。

 

 

 

#ifndef _CCOBJECTEANIMATE_H_ #define _CCOBJECTEANIMATE_H_ #include"cocos2d.h" using namespace cocos2d; class CCObjectAnimate:public CCNode { public:     static CCObjectAnimate* objectAnimate(char* objName);     void animationDone();     CCSprite* getObj();     void AnimationPlay(char* objName,bool isPlayAnim);     void AnimationPlayDone(); protected:     CCSprite* obj;     bool isPlay;     CCAnimation* getAnimation(char* objName);     CCAnimate *getAnimate(char* objName);     bool init(char* objName); }; #endif//CCOBJECTANIMATE_H_   #include"Common\CCObjectAnimate.h"//70   #include"DBgame.h"  CCObjectAnimate* CCObjectAnimate::objectAnimate(char* objName) {     CCObjectAnimate *obj = new CCObjectAnimate();      if (obj &&obj->init(objName))     {         obj->autorelease();         return obj;     }     CC_SAFE_DELETE(obj);     return NULL; } bool CCObjectAnimate::init(char* objName) {     bool bRet = false;     do{         char objPicture[50];         sprintf(objPicture,"%s_0.png",objName);         //printf("objPicture:%s\n",objPicture);         //this->setAnchorPoint(CCPointZero);         //創建動畫         obj=CCSprite::spriteWithSpriteFrameName(objPicture);         //obj->setAnchorPoint(CCPointZero);         this->addChild(obj);         //printf("objNameInit\n");         //obj->runAction(CCRepeatForever::actionWithAction(getAnimate(objName)));         obj->setIsVisible(false);         obj->runAction(CCAnimate::actionWithAnimation(getAnimation(objName),true));         isPlay=true;           bRet=true;     }while(0);      return bRet; } CCAnimate*  CCObjectAnimate::getAnimate(char* objName) {     return sCCParsePlistAnimation->getAnimate(objName); } CCAnimation* CCObjectAnimate::getAnimation(char* objName) {     return sCCParsePlistAnimation->getAnimation(objName); } CCSprite* CCObjectAnimate::getObj() {     return this->obj; } void CCObjectAnimate::animationDone() {     obj->stopAllActions(); } void CCObjectAnimate::AnimationPlay(char* objName,bool isPlayAnim) {          obj->setIsVisible(isPlayAnim);     this->cleanup();     obj->cleanup();          CCAction *sequneceAction = CCSequence::actions(         getAnimate(objName),         CCCallFunc::actionWithTarget(this, callfunc_selector(CCObjectAnimate::AnimationPlayDone)),         NULL);      obj->runAction(sequneceAction); } void CCObjectAnimate::AnimationPlayDone() {     obj->setIsVisible(false); } 
long原創
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章