轉自:http://codingnow.cn/cocos2d-x/810.html
這一篇來學習怎么使用cocos2d-x引擎播放幀動畫,就是把一幀一幀的圖片像電影那樣顯示出來。
1. 首先來了解一下相關的類
CCAnimation:是精靈用來播放動畫的參數,內部封裝了一個幀序列(CCMutableArray<CCSpriteFrame*>)和每幀播放間隔時間(float m_fDelay),初始化該對象時記得指定delay時間,否則默認是0。
CCAnimationCache:從名字很容易看出,它是用來緩存CCAnimation的,內部封裝了一個字典(CCMutableDictionary<std::string, CCAnimation*>),是一個單例。使用實例:
CCMutableArray<CCSpriteFrame *>* pAnimFrames = new CCMutableArray<CCSpriteFrame *>(10); /** .... */ CCAnimation* pAnimation = CCAnimation::animationWithFrames( pAnimFrames, 0.12f); //添加到緩存 CCAnimationCache::sharedAnimationCache()->addAnimation(pAnimation,"conch_animation"); //從緩存取出 CCAnimation* pAnimation2 =CCAnimationCache::sharedAnimationCache()->animationByName("conch_animation")
CCAnimate:它的父類是CCActionInterval,作用是根據CCAnimation封裝的幀序列和間隔時間,使精靈產生動畫效果。
cocos2d-x播放幀動畫的主要的流程是:
(1)創建CCSpriteFrame數組,可以使用CCSpriteFrameCache或者CCTextureCache。
(2)通過幀序列創建CCAnimation對象
(3)通過CCAnimation對象和間隔時間創建CCAnimate,生成一個持續性動作。
(4)使用精靈執行動作
2.下面分別使用CCSpriteFrameCache和CCTextureCache來實現一個播放動畫的demo
(1)使用CCSpriteFrameCache獲得動畫幀,可以使用TexturePacker工具把多個分散的小圖集中到一張大圖上,然后生成plist文件。程序中使用的圖片:

下面是播放動畫的核心代碼:
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCSpriteFrameCache* pFrameCache = CCSpriteFrameCache::sharedSpriteFrameCache(); pFrameCache->addSpriteFramesWithFile(s_plistPathPropConch, s_imgPathPropConch); m_pHero = CCSprite::spriteWithSpriteFrameName("ani_conch_1.png"); m_pHero->retain(); this->addChild(m_pHero); m_pHero->setPosition( ccp(winSize.width/2,winSize.height/2) ); const int frameCount = 4; CCMutableArray<CCSpriteFrame *>* pAnimFrames = new CCMutableArray<CCSpriteFrame *>(frameCount); char str[50] = {0}; for(int i = 0; i != frameCount; ++i) { sprintf(str, "ani_conch_%d.png", i+1); CCLog("str=%s",str); CCSpriteFrame* pFrame = pFrameCache->spriteFrameByName( str ); pAnimFrames->addObject( pFrame ); } //delay默認是0 CCAnimation* pAnimation = CCAnimation::animationWithFrames( pAnimFrames, 0.12f); pAnimFrames->release(); m_pHero->runAction( CCRepeatForever::actionWithAction( CCAnimate::actionWithAnimation(pAnimation,false) ) );
sprintf的作用是字符串格式化,主要功能是把格式化的數據寫入某個字符串中。sprintf(str, “ani_conch_%d.png”, 1)后str的值就變成了:ani_conch_1.png,其他使用方法可以去google或者百度查。
(2)使用CCTextureCache獲得動畫幀,程序中用到的圖片:

核心代碼如下:
CCSize winSize = CCDirector::sharedDirector()->getWinSize(); CCTexture2D* pTexture = CCTextureCache::sharedTextureCache()->addImage( s_imgPathKnight ); int textureWidth = pTexture->getContentSize().width; int textureHeight = pTexture->getContentSize().height; CCLog("width=%d,height=%d",textureWidth, textureHeight); const int row = 4,col = 4; int spriteWidth = textureWidth / row, spriteHeight = textureHeight / 4; CCSpriteFrame* pHeroFrame = CCSpriteFrame::frameWithTexture(pTexture,CCRectMake(0,0,spriteWidth,spriteHeight)); m_pHero = CCSprite::spriteWithSpriteFrame(pHeroFrame); m_pHero->retain(); this->addChild(m_pHero); m_pHero->setPosition( ccp(winSize.width/2,winSize.height/2) ); CCMutableArray<CCSpriteFrame *>* pAnimFrames = new CCMutableArray<CCSpriteFrame *>(16); for(int i = 0; i != col; ++i) { CCSpriteFrame* pSpriteFrame = CCSpriteFrame::frameWithTexture(pTexture,CCRectMake(spriteWidth * i, 0, spriteWidth, spriteHeight)); pAnimFrames->addObject(pSpriteFrame); } //delay默認是0 CCAnimation* pAnimation = CCAnimation::animationWithFrames( pAnimFrames, 0.2f); pAnimFrames->release(); m_pHero->runAction( CCRepeatForever::actionWithAction( CCAnimate::actionWithAnimation(pAnimation,false) ) );
