您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
代码出自魔塔
使用方法在代码后几行
- //**********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进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。