cocos2d-x 自建动画管理器AnimationManager的方法和使用
更新:HHH   时间:2023-1-7


代码出自魔塔

使用方法在代码后几行

  1. //**********animation.h**************  
  2. #ifndef _ANIMATION_MANAGER_H_  
  3. #define _ANIMATION_MANAGER_H_  
  4.  
  5. #include "MTGame.h"  
  6.  
  7. using namespace cocos2d;  
  8. //地方写的有点不太对,写在一个全局变量里比较好  
  9. typedef enum {  
  10.     kDown =  0,//向下方向  
  11.     kLeft = 1,//向左方向  
  12.     kRight= 2,//向右方向  
  13.     kUp = 3,//向上方向  
  14.     kNormal,  
  15. } HeroDirection;//勇士方向  
  16.  
  17. typedef enum 
  18. {  
  19.     kNone = 1,//可以通行  
  20.     kWall,//墙  
  21.     kEnemy,//敌人  
  22.     kItem,//物品  
  23.     kDoor,//门  
  24.     kNPC,//npc  
  25.     kTeleport,//传送点  
  26. } CollisionType;//碰撞类型  
  27.  
  28. typedef enum 
  29. {  
  30.     aDown = 0,//向下行走动画  
  31.     aLeft,//向左行走动画  
  32.     aRight,//向右行走动画  
  33.     aUp,//向上行走动画  
  34.     aFight,//刀光动画  
  35. } AnimationKey;//动画模版键值  
  36.  
  37. class AnimationManager : public Singleton<AnimationManager>  
  38. {  
  39. public:  
  40.     AnimationManager();  
  41.     ~AnimationManager();  
  42.     //初始化动画模版缓存表  
  43.     bool initAnimationMap();  
  44.     //根据名字得到一个动画模板  
  45.     CCAnimation* getAnimation(int key);  
  46.     //创建一个动画实例  
  47.     CCAnimate* createAnimate(int key);  
  48.     //创建一个动画实例  
  49.     CCAnimate* createAnimate(const char* key);  
  50. protected:  
  51.     //加载勇士行走动画模版  
  52.     CCAnimation* createHeroMovingAnimationByDirection(HeroDirection direction);  
  53.     CCAnimation* createFightAnimation();  
  54.     CCAnimation* createNPCAnimation();  
  55. };  
  56. //定义动画管理器实例的别名  
  57. #define sAnimationMgr AnimationManager::instance()  
  58.  
  59. #endif  
  60. //*********************animation.cpp**************************  
  61. #include "AnimationManager.h"  
  62.  
  63. DECLARE_SINGLETON_MEMBER(AnimationManager);  
  64.  
  65. AnimationManager::AnimationManager()  
  66. {  
  67. }  
  68.  
  69. AnimationManager::~AnimationManager()  
  70. {  
  71.     //CCDirector会自己清除AnimationCache  
  72.     //CCAnimationCache::purgeSharedAnimationCache();  
  73. }  
  74.  
  75. bool AnimationManager::initAnimationMap()  
  76. {  
  77.     char temp[20];  
  78.     sprintf(temp, "%d", aDown);  
  79.     //加载勇士向下走的动画  
  80.     CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kDown), temp);  
  81.     sprintf(temp, "%d", aRight);  
  82.     //加载勇士向右走的动画  
  83.     CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kRight), temp);  
  84.     sprintf(temp, "%d", aLeft);  
  85.     //加载勇士向左走的动画  
  86.     CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kLeft), temp);  
  87.     sprintf(temp, "%d", aUp);  
  88.     //加载勇士向上走的动画  
  89.     CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kUp), temp);  
  90.     //加载战斗动画  
  91.     sprintf(temp, "%d", aFight);  
  92.     CCAnimationCache::sharedAnimationCache()->addAnimation(createFightAnimation(), temp);  
  93.     //加载NPC动画  
  94.     CCAnimationCache::sharedAnimationCache()->addAnimation(createNPCAnimation(), "npc0");  
  95.     return true;  
  96. }  
  97.  
  98. CCAnimation* AnimationManager::createHeroMovingAnimationByDirection(HeroDirection direction)  
  99. {  
  100.     CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("hero.png");  
  101.     CCSpriteFrame *frame0, *frame1, *frame2, *frame3;  
  102.     //第二个参数表示显示区域的x, y, width, height,根据direction来确定显示的y坐标  
  103.     frame0 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*0, 32*direction, 32, 32));  
  104.     frame1 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*1, 32*direction, 32, 32));  
  105.     frame2 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*2, 32*direction, 32, 32));  
  106.     frame3 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*3, 32*direction, 32, 32));  
  107.     CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(4);  
  108.     animFrames->addObject(frame0);  
  109.     animFrames->addObject(frame1);  
  110.     animFrames->addObject(frame2);  
  111.     animFrames->addObject(frame3);  
  112.     CCAnimation* animation = new CCAnimation();  
  113.     //0.05f表示每帧动画间的间隔  
  114.     animation->initWithFrames(animFrames, 0.05f);  
  115.     animFrames->release();  
  116.  
  117.     return animation;  
  118. }  
  119.  
  120. //创建战斗动画模板  
  121. CCAnimation* AnimationManager::createFightAnimation()  
  122. {  
  123.     //定义每帧的序号  
  124.     int fightAnim[] =   
  125.     {  
  126.         4,6,8,10,13,15,17,19,20,22  
  127.     };  
  128.     CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>();  
  129.     CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("sword.png");  
  130.     CCSpriteFrame *frame;  
  131.     int x, y;  
  132.     for (int i = 0; i < 10; i++)   
  133.     {  
  134.         //计算每帧在整个纹理中的偏移量  
  135.         x = fightAnim[i] % 5 - 1;  
  136.         y = fightAnim[i] / 5;  
  137.         frame = CCSpriteFrame::frameWithTexture(texture, cocos2d::CCRectMake(192*x, 192*y, 192, 192));  
  138.         //第17和19帧在y方向上有-8的偏移  
  139.         if (fightAnim[i] == 17 || fightAnim[i] == 19)  
  140.         {  
  141.             frame->setOffsetInPixels( ccp(0, -8) );  
  142.         }  
  143.         animFrames->addObject(frame);  
  144.     }  
  145.     CCAnimation* animation = new CCAnimation();  
  146.     animation->initWithFrames(animFrames, 0.1f);  
  147.     animFrames->release();  
  148.     return animation;  
  149. }  
  150.  
  151. CCAnimation* AnimationManager::createNPCAnimation()  
  152. {  
  153.     CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("npc.png");  
  154.     CCSpriteFrame *frame0, *frame1, *frame2, *frame3;  
  155.     //第二个参数表示显示区域的x, y, width, height,根据direction来确定显示的y坐标  
  156.     frame0 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*0, 0, 32, 32));  
  157.     frame1 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*1, 0, 32, 32));  
  158.     frame2 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*2, 0, 32, 32));  
  159.     frame3 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*3, 0, 32, 32));  
  160.     CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(4);  
  161.     animFrames->addObject(frame0);  
  162.     animFrames->addObject(frame1);  
  163.     animFrames->addObject(frame2);  
  164.     animFrames->addObject(frame3);  
  165.     CCAnimation* animation = new CCAnimation();  
  166.     //0.05f表示每帧动画间的间隔  
  167.     animation->initWithFrames(animFrames, 0.2f);  
  168.     animFrames->release();  
  169.  
  170.     return animation;  
  171. }  
  172.  
  173. //获取指定动画模版  
  174. CCAnimation* AnimationManager::getAnimation(int key)  
  175. {  
  176.     char temp[20];  
  177.     sprintf(temp, "%d", key);  
  178.     return CCAnimationCache::sharedAnimationCache()->animationByName(temp);  
  179. }  
  180.  
  181. //获取一个指定动画模版的实例  
  182. CCAnimate* AnimationManager::createAnimate(int key)  
  183. {  
  184.     //获取指定动画模版  
  185.     CCAnimation* anim = getAnimation(key);  
  186.     if(anim)  
  187.     {  
  188.         //根据动画模版生成一个动画实例  
  189.         return cocos2d::CCAnimate::actionWithAnimation(anim);  
  190.     }  
  191.     return NULL;  
  192. }  
  193.  
  194. //获取一个指定动画模版的实例  
  195. CCAnimate* AnimationManager::createAnimate(const char* key)  
  196. {  
  197.     //获取指定动画模版  
  198.     CCAnimation* anim = CCAnimationCache::sharedAnimationCache()->animationByName(key);  
  199.     if(anim)  
  200.     {  
  201.         //根据动画模版生成一个动画实例  
  202.         return cocos2d::CCAnimate::actionWithAnimation(anim);  
  203.     }  
  204.     return NULL;  
  205. }  
  206. //***********************调用animationManager的方法  
  207. //从动画管理器中根据npcId获取动画,开始永久播放  
  208.     CCAnimate* animation = sAnimationMgr->createAnimate(npcId->m_sString.c_str());  
  209.     if (animation != NULL) {  
  210.         CCActionInterval* action = CCRepeatForever::actionWithAction(animation);  
  211.         npcSprite->runAction(action);  
  212.     }  
  213.     ////////////////////////////////////////////////////////////////  
  214. //**************************动画管理器的使用**************************************  
  215. ////////////////////////////////////////////////////////////////////////////////      
  216. //*************** 第一步:appdelegate,中的初始化的方法*************  
  217. bool AppDelegate::applicationDidFinishLaunching()  
  218. {  
  219.     CCDirector *pDirector = CCDirector::sharedDirector();  
  220.     pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());  
  221.     pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);  
  222.     pDirector->setDisplayFPS(true);  
  223.     pDirector->setAnimationInterval(1.0 / 60);  
  224.     //初始化动画管理器  
  225.     sAnimationMgr->initAnimationMap();  
  226.     //创建游戏主界面  
  227.     CCScene *pScene = GameScene::playNewGame();  
  228.     //让director运行场景  
  229.     pDirector->runWithScene(pScene);  
  230.     return true;  
  231. }  
  232. //*******************最后一步:释放动画管理器**********************  
  233. AppDelegate::~AppDelegate()  
  234. {  
  235.     SimpleAudioEngine::end();  
  236.     //释放动画管理器  
  237.     sAnimationMgr->release();  
  238. }  
  239.  
  240. //******************************第二步:使用方法***********************************  
  241.  
  242. //**************初始化一个精灵1********************  
  243. //用模板初始化一个精灵  
  244. bool Hero::heroInit()  
  245. {  
  246.       
  247.     heroSprite = CCSprite::spriteWithSpriteFrame(sAnimationMgr->getAnimation(kDown)->getFrames()->getObjectAtIndex(0));  
  248. }  
  249. //********************使用方法2******************************  
  250. //例子1  
  251.     heroSprite->runAction(sAnimationMgr->createAnimate(direction));  
  252. //例子2  
  253. CCAction* action = CCSequence::actions(  
  254.         sAnimationMgr->createAnimate(aFight),  
  255.         CCCallFuncN::actionWithTarget(this, callfuncN_selector(Hero::onFightDone)),  
  256.         NULL);  
  257. //例子3  
  258. //从动画管理器中根据npcId获取动画,开始永久播放  
  259.     CCAnimate* animation = sAnimationMgr->createAnimate(npcId->m_sString.c_str());  
  260.     if (animation != NULL) {  
  261.         CCActionInterval* action = CCRepeatForever::actionWithAction(animation);  
  262.         npcSprite->runAction(action);  
  263.     }  
  264.  
  265.  
  266.  

 

 

返回游戏开发教程...