您好,登錄后才能下訂單哦!
代碼出自魔塔
使用方法在代碼后幾行
- //**********animation.h**************
- #ifndef _ANIMATION_MANAGER_H_
- #define _ANIMATION_MANAGER_H_
- #include "MTGame.h"
- using namespace cocos2d;
- //地方寫的有點不太對,寫在一個全局變量里比較好
- typedef enum {
- kDown = 0,//向下方向
- kLeft = 1,//向左方向
- kRight= 2,//向右方向
- kUp = 3,//向上方向
- kNormal,
- } HeroDirection;//勇士方向
- typedef enum
- {
- kNone = 1,//可以通行
- kWall,//墻
- kEnemy,//敵人
- kItem,//物品
- kDoor,//門
- kNPC,//npc
- kTeleport,//傳送點
- } CollisionType;//碰撞類型
- typedef enum
- {
- aDown = 0,//向下行走動畫
- aLeft,//向左行走動畫
- aRight,//向右行走動畫
- aUp,//向上行走動畫
- aFight,//刀光動畫
- } AnimationKey;//動畫模版鍵值
- class AnimationManager : public Singleton<AnimationManager>
- {
- public:
- AnimationManager();
- ~AnimationManager();
- //初始化動畫模版緩存表
- bool initAnimationMap();
- //根據名字得到一個動畫模板
- CCAnimation* getAnimation(int key);
- //創建一個動畫實例
- CCAnimate* createAnimate(int key);
- //創建一個動畫實例
- CCAnimate* createAnimate(const char* key);
- protected:
- //加載勇士行走動畫模版
- CCAnimation* createHeroMovingAnimationByDirection(HeroDirection direction);
- CCAnimation* createFightAnimation();
- CCAnimation* createNPCAnimation();
- };
- //定義動畫管理器實例的別名
- #define sAnimationMgr AnimationManager::instance()
- #endif
- //*********************animation.cpp**************************
- #include "AnimationManager.h"
- DECLARE_SINGLETON_MEMBER(AnimationManager);
- AnimationManager::AnimationManager()
- {
- }
- AnimationManager::~AnimationManager()
- {
- //CCDirector會自己清除AnimationCache
- //CCAnimationCache::purgeSharedAnimationCache();
- }
- bool AnimationManager::initAnimationMap()
- {
- char temp[20];
- sprintf(temp, "%d", aDown);
- //加載勇士向下走的動畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kDown), temp);
- sprintf(temp, "%d", aRight);
- //加載勇士向右走的動畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kRight), temp);
- sprintf(temp, "%d", aLeft);
- //加載勇士向左走的動畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kLeft), temp);
- sprintf(temp, "%d", aUp);
- //加載勇士向上走的動畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createHeroMovingAnimationByDirection(kUp), temp);
- //加載戰斗動畫
- sprintf(temp, "%d", aFight);
- CCAnimationCache::sharedAnimationCache()->addAnimation(createFightAnimation(), temp);
- //加載NPC動畫
- CCAnimationCache::sharedAnimationCache()->addAnimation(createNPCAnimation(), "npc0");
- return true;
- }
- CCAnimation* AnimationManager::createHeroMovingAnimationByDirection(HeroDirection direction)
- {
- CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("hero.png");
- CCSpriteFrame *frame0, *frame1, *frame2, *frame3;
- //第二個參數表示顯示區域的x, y, width, height,根據direction來確定顯示的y坐標
- frame0 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*0, 32*direction, 32, 32));
- frame1 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*1, 32*direction, 32, 32));
- frame2 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*2, 32*direction, 32, 32));
- frame3 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*3, 32*direction, 32, 32));
- CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(4);
- animFrames->addObject(frame0);
- animFrames->addObject(frame1);
- animFrames->addObject(frame2);
- animFrames->addObject(frame3);
- CCAnimation* animation = new CCAnimation();
- //0.05f表示每幀動畫間的間隔
- animation->initWithFrames(animFrames, 0.05f);
- animFrames->release();
- return animation;
- }
- //創建戰斗動畫模板
- CCAnimation* AnimationManager::createFightAnimation()
- {
- //定義每幀的序號
- int fightAnim[] =
- {
- 4,6,8,10,13,15,17,19,20,22
- };
- CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>();
- CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("sword.png");
- CCSpriteFrame *frame;
- int x, y;
- for (int i = 0; i < 10; i++)
- {
- //計算每幀在整個紋理中的偏移量
- x = fightAnim[i] % 5 - 1;
- y = fightAnim[i] / 5;
- frame = CCSpriteFrame::frameWithTexture(texture, cocos2d::CCRectMake(192*x, 192*y, 192, 192));
- //第17和19幀在y方向上有-8的偏移
- if (fightAnim[i] == 17 || fightAnim[i] == 19)
- {
- frame->setOffsetInPixels( ccp(0, -8) );
- }
- animFrames->addObject(frame);
- }
- CCAnimation* animation = new CCAnimation();
- animation->initWithFrames(animFrames, 0.1f);
- animFrames->release();
- return animation;
- }
- CCAnimation* AnimationManager::createNPCAnimation()
- {
- CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("npc.png");
- CCSpriteFrame *frame0, *frame1, *frame2, *frame3;
- //第二個參數表示顯示區域的x, y, width, height,根據direction來確定顯示的y坐標
- frame0 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*0, 0, 32, 32));
- frame1 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*1, 0, 32, 32));
- frame2 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*2, 0, 32, 32));
- frame3 = CCSpriteFrame::frameWithTexture(heroTexture, cocos2d::CCRectMake(32*3, 0, 32, 32));
- CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(4);
- animFrames->addObject(frame0);
- animFrames->addObject(frame1);
- animFrames->addObject(frame2);
- animFrames->addObject(frame3);
- CCAnimation* animation = new CCAnimation();
- //0.05f表示每幀動畫間的間隔
- animation->initWithFrames(animFrames, 0.2f);
- animFrames->release();
- return animation;
- }
- //獲取指定動畫模版
- CCAnimation* AnimationManager::getAnimation(int key)
- {
- char temp[20];
- sprintf(temp, "%d", key);
- return CCAnimationCache::sharedAnimationCache()->animationByName(temp);
- }
- //獲取一個指定動畫模版的實例
- CCAnimate* AnimationManager::createAnimate(int key)
- {
- //獲取指定動畫模版
- CCAnimation* anim = getAnimation(key);
- if(anim)
- {
- //根據動畫模版生成一個動畫實例
- return cocos2d::CCAnimate::actionWithAnimation(anim);
- }
- return NULL;
- }
- //獲取一個指定動畫模版的實例
- CCAnimate* AnimationManager::createAnimate(const char* key)
- {
- //獲取指定動畫模版
- CCAnimation* anim = CCAnimationCache::sharedAnimationCache()->animationByName(key);
- if(anim)
- {
- //根據動畫模版生成一個動畫實例
- return cocos2d::CCAnimate::actionWithAnimation(anim);
- }
- return NULL;
- }
- //***********************調用animationManager的方法
- //從動畫管理器中根據npcId獲取動畫,開始永久播放
- CCAnimate* animation = sAnimationMgr->createAnimate(npcId->m_sString.c_str());
- if (animation != NULL) {
- CCActionInterval* action = CCRepeatForever::actionWithAction(animation);
- npcSprite->runAction(action);
- }
- ////////////////////////////////////////////////////////////////
- //**************************動畫管理器的使用**************************************
- ////////////////////////////////////////////////////////////////////////////////
- //*************** 第一步:appdelegate,中的初始化的方法*************
- bool AppDelegate::applicationDidFinishLaunching()
- {
- CCDirector *pDirector = CCDirector::sharedDirector();
- pDirector->setOpenGLView(&CCEGLView::sharedOpenGLView());
- pDirector->setDeviceOrientation(kCCDeviceOrientationLandscapeLeft);
- pDirector->setDisplayFPS(true);
- pDirector->setAnimationInterval(1.0 / 60);
- //初始化動畫管理器
- sAnimationMgr->initAnimationMap();
- //創建游戲主界面
- CCScene *pScene = GameScene::playNewGame();
- //讓director運行場景
- pDirector->runWithScene(pScene);
- return true;
- }
- //*******************最后一步:釋放動畫管理器**********************
- AppDelegate::~AppDelegate()
- {
- SimpleAudioEngine::end();
- //釋放動畫管理器
- sAnimationMgr->release();
- }
- //******************************第二步:使用方法***********************************
- //**************初始化一個精靈1********************
- //用模板初始化一個精靈
- bool Hero::heroInit()
- {
- heroSprite = CCSprite::spriteWithSpriteFrame(sAnimationMgr->getAnimation(kDown)->getFrames()->getObjectAtIndex(0));
- }
- //********************使用方法2******************************
- //例子1
- heroSprite->runAction(sAnimationMgr->createAnimate(direction));
- //例子2
- CCAction* action = CCSequence::actions(
- sAnimationMgr->createAnimate(aFight),
- CCCallFuncN::actionWithTarget(this, callfuncN_selector(Hero::onFightDone)),
- NULL);
- //例子3
- //從動畫管理器中根據npcId獲取動畫,開始永久播放
- CCAnimate* animation = sAnimationMgr->createAnimate(npcId->m_sString.c_str());
- if (animation != NULL) {
- CCActionInterval* action = CCRepeatForever::actionWithAction(animation);
- npcSprite->runAction(action);
- }
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。