cocos2dx動畫Animation介紹

2021-07-05 22:37:23 字數 4240 閱讀 8826

一、幀動畫

你可以通過一系列檔案,像如下這樣,建立乙個動畫:

01ccanimation *animation = ccanimation::create();

02//從本地檔案系統中載入檔案到ccspriteframe中區,然後新增到ccanimation中

03for(inti = 1; i < 15; i++)

04;

06sprintf(szimagefilename,"images/grossini_dance_%02d.png", i);

07animation->addspriteframewithfilename(szimagefilename);

08}

09animation->setdelayperunit(2.8f / 14.0f);// 這個動畫包含14幀,將會持續2.8秒.

10animation->setrestoreoriginalframe(true);

11

12ccanimate *action = ccanimate::create(animation);

13sprite->runaction(action);// 執行精靈物件

注意ccanimation是由許多精靈幀組成,可以設定間隔時間,持續時間等,它實際上是包含著一組資料。ccanimate是乙個動作,它是通過ccanimation物件建立。

二、精靈表動畫

儘管手工動畫很容易理解,但它很少用在遊戲開發中。相反的,精靈表動畫的方式在2d動畫中經常使用。

這是乙個精靈表。它實際上就是一系列動畫幀,或者是乙個能用於乙個場景的集。

在opengles1.1階段,精靈表因為以下幾點被廣泛應用:

1、減少檔案讀寫時間。讀取一張比讀取一堆小檔案肯定要快。

2、減少記憶體消耗。opengl es 1.1僅僅能夠使用2的幾次方大小的(也就是寬度或者高度是2,4,864,128,256,512,1024,…)。也就是說,opengl es1.1將會分配給每個2的幾次方大小的記憶體空間,即使你這張達不到這樣的寬度和高度也會分配大於此的2的n次方大小的空間。那麼運用這種集的方式將會減少記憶體碎片。

3、減少opengl es繪製呼叫並且加速渲染。

cocos2d-x v2.0公升級到了opengl es2.0.opengl es2.0不會再分配2的幾次方的記憶體塊了,但是減少讀取時間和繪製呼叫的優勢依然存在。

那麼生成的動畫效果如何呢?正如我們所見,精靈表不是動畫的乙個必須條件。但是考慮到以上的一些優勢,精靈表還是很有效率的。在cocos2dx中,有許多不同的方式來建立精靈表。

三、通過.png 和 .plist檔案建立精靈表

在cocos2dx 0.x和1.x版本中,ccspritesheet就是為以上的目的設計。在v2.0中ccspritebatchnode替代了ccspritesheet。

ccspritebatchnode物件包含了所有精靈幀的紋理。即使它不會繪製,你也必須要把它新增到場景中,例如:

1ccspritebatchnode* spritebatch = ccspritebatchnode::create("animations/grossini.png");

下一步,你需要運用ccspriteframecache例項來確保幀名字對應幀邊界。也就是說,在哪一塊矩形區域中。例如:

1ccspriteframecache* cache = ccspriteframecache::sharedspriteframecache();

2cache->addspriteframeswithfile("animations/grossini.plist");

一旦你的精靈表和幀載入完成,並且精靈表已經被新增到了場景中,你可以通過createwithspriteframename方法來建立精靈。並且通過addchild要新增到精靈表中:

1m_psprite1 = ccsprite::createwithspriteframename("grossini_dance_01.png");

2spritebatch->addchild(m_psprite1);

3addchild(spritebatch);

createwithspriteframename 方法將會從grossini.plist中找到對應的座標以及矩形區域,之後再裁剪grossini.png的紋理成乙個精靈幀。

現在我們建立乙個ccarray物件並且天劍所有的幀動畫進去。在這個動畫的例子中,我們發現所有的14幀都有相同的大小,所以我們可以用乙個巢狀的迴圈遍歷它們,並且當完成新增14幀之後結束掉迴圈。

1ccarray* animframes = ccarray::createwithcapacity(15);

2charstr[100] = ;

3

4for(inti = 1; i < 15; i++)

5

最後,我們需要建立乙個ccanimate動作例項來執行ccsprite。下面我們可以在ccrepeatforever動作中包裹ccanimate動作來讓它一直執行下去,像這樣:

1ccanimation* animation = ccanimation::createwithspriteframes(animframes, 0.3f);

2m_psprite1->runaction( ccrepeatforever::create( ccanimate::create(animation) ) );

四、檔案動畫

ccanimatecache能夠載入乙個描述一批節點的xml/plist檔案,包括幀名和他們的矩形區域。這個藉口非常容易使用。

1ccanimationcache *cache = ccanimationcache::sharedanimationcache();// 快取在cocos2dx中一直是單例模式

2cache->addanimationswithfile("animations/animations-2.plist");

3ccanimation animation = cache->animationbyname("dance_1");

4ccanimate animate = ccanimate::create(animation);

5sprite->runaction(animate);

cocos2dx開發網

cocos2dx動畫Animation介紹

一 幀動畫 cpp ccanimation animation ccanimation create 從本地檔案 系統中載入檔案到ccspriteframe中區,然後新增到ccanimation中 for int i 1 i 15 i sprintf szimagefilename,images g...

Cocos2dx 《基礎》 幀動畫

幀動畫 a.spriteframe 精靈幀。精靈幀包含了對應紋理在大的紋理區域中的位置和大小,對應紋理是否經過旋轉和偏移。根據這些幾何資訊,可以從大的紋理中找到正確的紋理區域作 為精靈幀顯示的影象。使用紋理建立精靈幀 auto tex texturecache getinstance addimag...

cocos2dx動畫Animation介紹

一 幀動畫 你可以通過一系列檔案,像如下這樣,建立乙個動畫 cpp ccanimation animation ccanimation create 從本地檔案系統中載入檔案到ccspriteframe中區,然後新增到ccanimation中 for int i 1 i 15 i sprintf s...