代码出自魔塔
使用方法在代码后几行
-
- #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,
- 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
-
- #include "AnimationManager.h"
-
- DECLARE_SINGLETON_MEMBER(AnimationManager);
-
- AnimationManager::AnimationManager()
- {
- }
-
- AnimationManager::~AnimationManager()
- {
-
-
- }
-
- 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);
-
- CCAnimationCache::sharedAnimationCache()->addAnimation(createNPCAnimation(), "npc0");
- return true;
- }
-
- CCAnimation* AnimationManager::createHeroMovingAnimationByDirection(HeroDirection direction)
- {
- CCTexture2D *heroTexture = CCTextureCache::sharedTextureCache()->addImage("hero.png");
- CCSpriteFrame *frame0, *frame1, *frame2, *frame3;
-
- 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();
-
- 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));
-
- 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;
-
- 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();
-
- 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;
- }
-
-
- CCAnimate* animation = sAnimationMgr->createAnimate(npcId->m_sString.c_str());
- if (animation != NULL) {
- CCActionInterval* action = CCRepeatForever::actionWithAction(animation);
- npcSprite->runAction(action);
- }
-
-
-
-
- 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();
-
- pDirector->runWithScene(pScene);
- return true;
- }
-
- AppDelegate::~AppDelegate()
- {
- SimpleAudioEngine::end();
-
- sAnimationMgr->release();
- }
-
-
-
-
-
- bool Hero::heroInit()
- {
-
- heroSprite = CCSprite::spriteWithSpriteFrame(sAnimationMgr->getAnimation(kDown)->getFrames()->getObjectAtIndex(0));
- }
-
-
- heroSprite->runAction(sAnimationMgr->createAnimate(direction));
-
- CCAction* action = CCSequence::actions(
- sAnimationMgr->createAnimate(aFight),
- CCCallFuncN::actionWithTarget(this, callfuncN_selector(Hero::onFightDone)),
- NULL);
-
-
- CCAnimate* animation = sAnimationMgr->createAnimate(npcId->m_sString.c_str());
- if (animation != NULL) {
- CCActionInterval* action = CCRepeatForever::actionWithAction(animation);
- npcSprite->runAction(action);
- }
-
-
-