您的位置:首页 > 移动开发 > Cocos引擎

Cocos2d-x学习笔记之动画的处理

2012-11-01 22:10 489 查看
第一步解析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) {     //缓冲——这会加载对应的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; }

接下来进行初始化在HelloWorldScene的init函数中初始化.
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原创
本文出自 “Cocos2D-X” 博客,请务必保留此出处http://lonag.blog.51cto.com/3340984/1047260
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: