Cocos2d-x學習筆記(四)淺析動作類CCAction(3)
【1】動作管理類CCActionManager:
動作管理類CCActionManager是一個管理所有動作的單例,可以對動作進行一系列的操作,如:新增動作、刪除動作、暫停動作、重啟動作,這個單例可以通過CCDirector::sharedDirector()->getActionManager()來獲得。事實上前面所說的 runAction方法只是將行為物件新增進CCActionManager的執行佇列。
CCActionManager的用法很簡單,只是通過一些函式管理一些動作而已,CCActionManager的主要函式為:
addAction方法: 為指定目標新增動作
removeAllAction方法: 刪除所有動作
removeAction方法: 刪除傳入的動作
removeActionByTag方法: 通過標籤刪除動作
getActionByTag方法: 通過標籤獲取動作
pauseTarget方法: 暫停目標動作
resumeTarget方法: 重啟目標動作
【2】網格動作CCGrid3D:
網格動作也是實現一些動作特效,主要基於網格實現。和基本動作的使用非常類似,需要注意的是在使用網格之前需要取消OpenGL的深度檢測,呼叫語句:
CCDirector::sharedDirector()->setDepthTest(false);
其具體子類可參考《Cocosd-x權威指南》,例項可參考TestCpp相關原始碼。
【3】動畫的製作與使用:
Cocos2d-x中有一種動作可以用來實現動畫——動畫動作CCAnimate。當然執行動畫動作需要動畫資源,動畫資源可通過動畫類CCAnimation來載入。建立動畫需要動畫幀,可通過單張圖片來建立,亦可通過精靈幀來建立。為了快取所有的動畫和動畫幀,需要使用CCAnimationCache類,它是一個單例,可由CCAnimationCache::sharedAnimationCache()獲取。弄懂test專案的SpriteTest檔案下的例項便知道怎麼使用了,下面是部分程式碼(省略相關定義及操作):
/*獲取精靈幀快取*/
CCSpriteFrameCache *frameCache = CCSpriteFrameCache::sharedSpriteFrameCache();
CCArray* animFrames = CCArray::createWithCapacity(15);//用於存放精靈幀
char str[100] = {0};
for(int i = 1; i < 15; i++)
{
sprintf(str, "grossini_dance_%02d.png",i);
/*通過圖片路徑獲取精靈幀物件*/
CCSpriteFrame *frame = frameCache->spriteFrameByName(str); animFrames->addObject(frame);
}
/*通過一系列精靈幀建立動畫*/
CCAnimation *animation = CCAnimation::createWithSpriteFrames(animFrames, 0.2f);
// Add an animation to the Cache
CCAnimationCache::sharedAnimationCache()->addAnimation(animation, "dance");
/*建立動畫動作*/
CCAnimate *animN = CCAnimate::create(normal);
CCAnimate *animG = CCAnimate::create(dance_grey);
CCAnimate *animB = CCAnimate::create(dance_blue);
/*組成動作系列*/
CCSequence *seq = CCSequence::create(animN, animG, animB, NULL);、
/*執行這一系列動作(動畫)*/
grossini->runAction(seq);