歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> cocos2d-x 創建幀動畫

cocos2d-x 創建幀動畫

日期:2017/3/1 10:41:43   编辑:Linux編程

看到好多人問如何用cocos2d-x創建幀動畫,其實用cocos2d-x很容易創建幀動畫。我就寫一遍吧。

  1. void MyClass::initMyAnim()
  2. {
  3. /**
  4. //可以在程序載入的時候預加載這些動畫資源,然後在cache中讀取
  5. CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
  6. cache->addSpriteFramesWithFile("run.plist", "run.png");
  7. */
  8. CCSpriteFrameCache* cache = CCSpriteFrameCache::sharedSpriteFrameCache();
  9. CCMutableArray<CCSpriteFrame*>* animFrames = new CCMutableArray<CCSpriteFrame*>(12);
  10. char str[64] = {0};
  11. for(int i = 1; i <=12; i++)
  12. {
  13. sprintf(str, "run%04d.png", i);
  14. CCSpriteFrame* frame = cache->spriteFrameByName( str );
  15. animFrames->addObject(frame);
  16. }
  17. CCAnimation* animation = CCAnimation::animationWithFrames(animFrames,0.4f);
  18. CCActionInterval* action=CCAnimate::actionWithAnimation(animation,true);
  19. CCFiniteTimeAction *myRun= CCSequence::actions(action,CCCallFunc::actionWithTarget(this,callfunc_selector(MyClass::callBackRun)),NULL);
  20. myRun->retain();
  21. animFrames->release();
  22. }

CCCallFunc用來做動畫回調用,沒有回調的話,就不用了。myRun->retain()後記得release掉。

OK,是不是很簡單。記錄一筆。

Copyright © Linux教程網 All Rights Reserved