ufolr原創,轉載請註明:
轉載自ufolr的博客 原文連接:http://blog.csdn.net/ufolr/article/details/7404403
前一篇教程,把項目中道具系統用到的很多東全揉搓在一起,要說明的很多,但東西實在太多,沒辦法一一道來。
現在我們就單獨的來看看coco2d中動畫的的基本實現。
利用上一篇教程中“定時炸彈”的動畫來說明吧,先上資源圖:
然後上代碼:
bool Daoju::ZhadanAnimate(float zx, float zy){
CCLog("Animate");
CCTexture2D *texture = CCTextureCache::sharedTextureCache()->addImage("Zhadan.png");
//加載資源(圖片起點_左上角,圖片大小)
//讀取資源中的每一幀,暫存到fram
CCSpriteFrame *frame0 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*0, 16, 16));
CCSpriteFrame *frame1 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*0, 16, 16));
CCSpriteFrame *frame2 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*1, 16, 16));
CCSpriteFrame *frame3 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*1, 16, 16));
CCSpriteFrame *frame4 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*2, 16, 16));
CCSpriteFrame *frame5 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*2, 16, 16));
CCSpriteFrame *frame6 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*0, 16*3, 16, 16));
CCSpriteFrame *frame7 = CCSpriteFrame::frameWithTexture(texture, CCRectMake(16*1, 16*3, 16, 16));
//設置初始圖片(第一幀)
CCSprite* spZhadananim = CCSprite::spriteWithSpriteFrame(frame0);
spZhadananim->setPosition(ccp(zx, zy));
addChild(spZhadananim, 0, 200);//加到相應圖層,設置標籤爲200,我們在回調函數中會利用標籤找到對應的精靈,然後刪除動畫精靈
//合成動畫-炸彈等待
CCMutableArray<CCSpriteFrame*> *animZhadanWaitFrames = new CCMutableArray<CCSpriteFrame*>(4);
//animZhadanWaitFrames->addObject(frame0);
//animZhadanWaitFrames->addObject(frame1);
//animZhadanWaitFrames->addObject(frame2);
//animZhadanWaitFrames->addObject(frame3);
animZhadanWaitFrames->addObject(frame4);
animZhadanWaitFrames->addObject(frame5);
animZhadanWaitFrames->addObject(frame6);
animZhadanWaitFrames->addObject(frame7);
//合成動畫-炸彈爆炸
CCMutableArray<CCSpriteFrame*> *animZhadanExciteFrames = new CCMutableArray<CCSpriteFrame*>(4);
animZhadanExciteFrames->addObject(frame0);
animZhadanExciteFrames->addObject(frame1);
animZhadanExciteFrames->addObject(frame2);
animZhadanExciteFrames->addObject(frame3);
//將4個動畫偵生成CCAnimation對象,每0.1秒(float)播放一幀
//生成炸彈等待動畫
CCAnimation *animationWait = CCAnimation::animationWithFrames(animZhadanWaitFrames, 0.1f);
CCAnimate *animateWait = CCAnimate::actionWithAnimation(animationWait, false);//動畫模版生成動畫
//生成炸彈激發動畫
CCAnimation *animationExcit = CCAnimation::animationWithFrames(animZhadanExciteFrames, 0.2f);
CCAnimate *animateExcit = CCAnimate::actionWithAnimation(animationExcit, false);//此處值爲false動畫播放完是在最後一幀,爲true則返回第一幀
//創建回調Action
CCCallFunc *animOverCallBack = CCCallFunc::actionWithTarget(this, callfunc_selector(Daoju::animBombsOverCallBack));
//依次執行Action
spZhadananim->runAction(CCSequence::actions(
CCRepeat::actionWithAction( animateWait, 12),//第一段動畫重複12次
//CCDelayTime::actionWithDuration(0.001f), //暫停瞬間一般情況下不用
CCRepeat::actionWithAction( animateExcit, 1), //第二段動畫重複1次
animOverCallBack,//回調函數,用於清屏,清除動畫,否則動畫播放玩後屏幕會殘留在屏幕上(對應上面的值可能是最後一幀也可能是第一幀)
NULL
));
animZhadanWaitFrames->release();//釋放資源
animZhadanExciteFrames->release();
return true;
}
未完待續。。