[cocos2dx笔记002] 坦克动画

[cocos2dx笔记002] 坦克动画
[cocos2dx笔记002] 坦克动画
第一:没有美术,只好自己用PS。先用PS在原来的4个方向上,做一个履带差异的4方向坦克。
第二:用SpritePacker打包成tankbase.plist。然后用CCSpriteFrameCache加载到内存中。
CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("basetank.plist");
<? xml version="1.0" encoding="UTF-8" ?>
<! DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd" >
< plist  version ="1.0" >
< dict >
     < key >frames </ key >
     < dict >
         < key >basetank_down.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
         < key >basetank_down_a.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,132},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
         < key >basetank_left.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,33},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
         < key >basetank_left_a.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,165},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
         < key >basetank_right.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,66},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
         < key >basetank_right_a.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,198},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
         < key >basetank_up.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,99},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
         < key >basetank_up_a.png </ key >
         < dict >
             < key >frame </ key >
             < string >{{0,231},{32,32}} </ string >
             < key >offset </ key >
             < string >{0,0} </ string >
             < key >rotated </ key >
             < false />
             < key >sourceColorRect </ key >
             < string >{{0,0},{32,32}} </ string >
             < key >sourceSize </ key >
             < string >{32,32} </ string >
         </ dict >
     </ dict >
     < key >metadata </ key >
     < dict >
         < key >format </ key >
         < integer >2 </ integer >
         < key >realTextureFileName </ key >
         < string >basetank.png </ string >
         < key >size </ key >
         < string >{32,512} </ string >
         < key >smartupdate </ key >
         < string >{None} </ string >
         < key >textureFileName </ key >
         < string >basetank.png </ string >
     </ dict >
</ dict >
</ plist >
第三:封装一个动画的函数
         /* *******************************************************************************
          Title: 生成指定方向的坦克动画Action
         
          FullName:   zdh::XGameGlobal::createBasetankAni
          Access:     public 
          @param [in] param_Direction     方向,参见EnumDirection,如果是无效方向,视为ED_UP
          @return     CCRepeatForever *   返回坦克动画的Action对象
          @author     zdhsoft
          @date       2014/03/24
          @file       xgameglobal.h
         *******************************************************************************
*/
        CCRepeatForever * createBasetankAni( int param_Direction)  const
        {
             // 创建帧资源的数组
            CCArray* st_frame_list =  new CCArray(2);
            CCSpriteFrame * pF1 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(getDirectionResName(param_Direction, 0));
            CCSpriteFrame * pF2 = CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(getDirectionResName(param_Direction, 1));
            st_frame_list->addObject(pF1);
            st_frame_list->addObject(pF2);

             // 创建动画对象 第二个参数是每帧间隔时间 这里是0.1秒
            CCAnimation * pAni = CCAnimation::createWithSpriteFrames(st_frame_list, 0.1f);
             // 创建动画Action
            CCAnimate * pAniAction = CCAnimate::create(pAni);
             // 创建重复Action
            CCRepeatForever *pAction = CCRepeatForever::create(pAniAction);
            
            st_frame_list->release();
             return pAction;
        }
第四:在玩家对象设置方向和对象初始化的时候,设置动画
初始化:
            CCSprite * p1Pic = CCSprite::createWithSpriteFrameName(GAME_GLOBAL->getDirectionResName(ED_UP));
            p1Pic->setPosition(GetPosition(pPlayerLayer, "Player1"));
            p1Pic->setAnchorPoint(ccp(0, 0));
             this->addChild(p1Pic);
            p1Pic->runAction(GAME_GLOBAL->createBasetankAni(ED_UP));
            m_Player1.setDraw(p1Pic);
设置方向:
         /* *******************************************************************************
          Title: 设置坦克的方向
         
          FullName:   zdh::XPlayer::setDirection
          Access:     public 
          @param [in] paramDirection 如果是无效的方向,会视为ED_UP
          @return     bool 
            - true   设置成功
            - false  设置失败
          @author     zdhsoft
          @date       2014/03/24
          @file       xplayer.h
         *******************************************************************************
*/
         bool setDirection( const  int paramDirection)
        {
             if (paramDirection >= ED_UP &&paramDirection <= ED_RIGHT)   // 如果是无效方向,则设置不成功
            {
                 if (paramDirection != m_Direction)
                {
                     if (m_Draw != nullptr)
                    {
                        m_Draw->stopAllActions();
                        m_Draw->setDisplayFrame(CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName(GAME_GLOBAL->getDirectionResName(paramDirection)));
                        m_Draw->runAction(GAME_GLOBAL->createBasetankAni(paramDirection));
                    }
                    m_Direction = paramDirection;  // 变更方向
                     return  true;
                }
            }
             return  false;
        }
这样就OK了!

你可能感兴趣的:([cocos2dx笔记002] 坦克动画)