在手機遊戲開發過程中,應用動畫效果是必須的。在cocos2d-x中,如何組織動畫,如下:
1.應用CCSpriteBatchNode,步驟如下:
1.1,使用CCSpriteFrameCache::sharedSpriteFrameCache()加載plist文件
1.2,使用CCSpriteBatchNode::create("Big_0.png", 34 ); 函數從本地媒介中加載一個被拼合過的大圖。這種方式有利於節省內存,提高渲染效果。
1.3,使用CCSpriteFrame::frameWithTexture函數,從CCSpriteBatchNode對象中加入一個指定位置,大小的圖片,然後放在一個數組中。
1.4,使用CCAnimation::createWithSpriteFrames,從精靈幀容器創建一個序列幀動畫。
1.5,使用CCAnimate::create(animatio );,從序列幀動畫中創建一個動畫動作,到動作之後,就可以使用這個動作了,然後在UI中,就可以看見這個動畫了。
例子,如下:
//第一步(1.1)
- CCSpriteFrameCache::sharedSpriteFrameCache()->addSpriteFramesWithFile("pd_sprites.plist");
- //pvr.ccz是由TexturePacker圖片處理應用後的大圖片。它將所有的相類的圖片,組成在一起,這樣一次性加載到內在中。
- //對機器的內在和性能有比較大的更善。
- _actors = CCSpriteBatchNode::create("pd_sprites.pvr.ccz");
- //消除齲齒
- _actors->getTexture()->setAliasTexParameters();
- this->addChild(_actors, -5);
//第二步(1.2)
- //創建精靈幀存放容器
- CCArray* tSpriteFrameArray = CCArray::create();
- //for循環進行讀取相應數量的紋理塊信息
- for(int b = 0 ; b < FrameNum; ++b)
- {
- //設置紋理不進行抗鋸齒模糊,像素精細
- pNewBatchNode->getTexture()->setAliasTexParameters();
- //設置從對應紋理塊中讀取出一個精靈幀
- CCSpriteFrame* tpSpriteFrame = CCSpriteFrame::frameWithTexture(_actors->getTexture(), tRect, tPackNode.m_bRotated, tOffset, tRect.size);
- //將精靈幀放入容器。
- tSpriteFrameArray->addObject(tpSpriteFrame);
- }
//第三步(1.3)
- //從精靈幀容器創建一個序列幀動畫。
- CCAnimation* animation = CCAnimation::createWithSpriteFrames(tSpriteFrameArray,1.0f);
- //設置每兩幀間時間間隔爲1秒。
- animation->setDelayPerUnit(1.0f);
- //設置動畫結束後仍保留動畫幀信息。
- animation->setRestoreOriginalFrame(true);
//第四步(1.4)
- //由這個動畫序列幀創建建動作
- CCAnimate* action = CCAnimate::create(animation);
- //創建精靈。
- CCSprite* pSprite = CCSprite::create();
- //設置精靈位置。
- pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));
//第五步(1.5)
- //讓演員演示這個動畫。
- pSprite->runAction(action);