cocos2d x 3 x 實現幀動畫的幾種方法

2021-08-19 13:39:36 字數 3218 閱讀 2140

方法一(動畫幀):
//建立乙個跑酷的精靈

auto sprite = sprite::create("1.png");


//設定精靈的座標


sprite->setposition(vec2(visiblesize.width/2,visiblesize.height/2));


//新增到當前層

this->addchild(sprite);


//建立序列幀動畫

auto animation = animation::create();


//設定動畫名字陣列的長度

char namesize[20] = ;


//動畫的迴圈 12張

for (int i =1; i<13; i++)


//設定動畫幀的時間間隔


animation->setdelayperunit(0.02f);


animation->setloops(-1);


//設定動畫結束後恢復到第一幀


animation->setrestoreoriginalframe(true);


//建立動畫動作

auto animate = animate::create(animation);


sprite->runaction(animate);

方法二(vector):
//幀動畫快取

auto framecache = spriteframecache::getinstance();


framecache02->addspriteframeswithfile("1.plist");


//建立乙個顯示動畫的精靈

auto sprite = sprite::createwithspriteframename("1.png");


//設定動畫的座標


sprite->setposition(vec2(visiblesize.width/2,visiblesize.height/2));


//新增到當前層

this->addchild(sprite);


//
建立乙個容器

vectorvec;


//設定動畫名字陣列的長度

char name[20] = ;


for (int i = 1; i<13; i++) 


//auto animation = animation::createwithspriteframes(vec,0.05f);

//也是可以這麼寫的。那setdelayperunit 這個需要注釋掉

auto animation = animation::createwithspriteframes(vec);


//設定動畫幀的時間間隔


animation->setdelayperunit(0.05f);


animation->setloops(-1);


//設定動畫結束後恢復到第一幀


animation->setrestoreoriginalframe(true);


//建立動畫動作

auto animate = animate::create(animation);


sprite->runaction(animate);

方法三(紋理):
//通過一張集合的來建立

//建立2d紋理

auto texture = director::getinstance()->gettexturecache()->addimage("dragon_animation.png");

//建立幀

auto frame0 = spriteframe::createwithtexture(texture, rect(132

*0, 132

*0, 132, 132));

auto frame1 = spriteframe::createwithtexture(texture, rect(132

*1, 132

*0, 132, 132));

auto frame2 = spriteframe::createwithtexture(texture, rect(132

*2, 132

*0, 132, 132));

auto frame3 = spriteframe::createwithtexture(texture, rect(132

*3, 132

*0, 132, 132));

auto frame4 = spriteframe::createwithtexture(texture, rect(132

*0, 132

*1, 132, 132));

auto frame5 = spriteframe::createwithtexture(texture, rect(132

*1, 132

*1, 132, 132));

auto sprite2 = sprite::createwithspriteframe(frame0);

sprite2->setposition(vec2(visiblesize.width/2, visiblesize.height/2));

this->addchild(sprite2);

//儲存幀

//vectorarray;

vector*>

array;

array

.pushback(frame0);

array

.pushback(frame1);

array

.pushback(frame2);

array

.pushback(frame3);

array

.pushback(frame4);

array

.pushback(frame5);

auto animation2 = animation::createwithspriteframes(array, 0.2f);

//此處createwithspriteframes()函式確實每幀間隔時間引數,需自行加上去!!!

sprite2->runaction(repeatforever::create(animate::create(animation2)));

cocos2d x 3 x 觸控響應

3.x需要自己註冊監聽事件。有兩種方式,乙個是用c 的bind繫結自定義函式,cocos2d x封裝了一下,提供了介面 cc callback 數字,數字代表引數個數。eventlistener需要兩個引數 touch 和 event 所以這裡使用的是cc callback 2 auto dispa...

cocos2d x 3 x記憶體管理

記憶體管理有ref提供的4個方法 void retain 將該物件的引用計數器 1 void release 將該物件的引用計數器 1 ref autorelease 不改變物件的引用計數器值,將物件新增到自動釋放池,返回物件本身 unsigned int getreferencecount con...

Cocos2dx 3 x多點觸控問題

首先,這並不是什麼教程。只是今天折騰了一天的乙個比較傻的問題。3.x的eventlistener想必各位已經都會了。toucheventallatonce是多點觸控,但幾乎沒什麼人用過。用法不難,但是很多人和我一樣卡在了無論怎麼搞touches的數量都只有一,換句話說,就是死活單點觸控。搜了很多論壇...