看到好多人問如何用cocos2d-x創建幀動畫,其實用cocos2d-x很容易創建幀動畫。我就寫一遍吧。
- void MyClass::initMyAnim()
- {
- /**
- //可以在程序載入的時候預加載這些動畫資源,然後在cache中讀取
- CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
- cache->addSpriteFramesWithFile("run.plist", "run.png");
- */
- CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
- CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(12);
-
- char str[64] = {0};
- for(int i = 1; i <=12; i++)
- {
- sprintf(str, "run%04d.png", i);
- CCSpriteFrame* frame = cache->spriteFrameByName( str );
- animFrames->addObject(frame);
- }
-
- CCAnimation* animation = CCAnimation::animationWithFrames(animFrames,0.4f);
- CCActionInterval* action=CCAnimate::actionWithAnimation(animation,true);
- CCFiniteTimeAction *myRun= CCSequence::actions(action,CCCallFunc::actionWithTarget(this,callfunc_selector(MyClass::callBackRun)),NULL);
- myRun->retain();
- animFrames->release();
- }
CCCallFunc用來做動畫回調用,沒有回調的話,就不用了。myRun->retain()後記得release掉。
OK,是不是很簡單。記錄一筆。