Cocos2d-x 自定义动画

来源:互联网 发布:孕囊第三个数据最大 编辑:程序博客网 时间:2024/05/16 04:43
出处:http://blog.csdn.net/zhy_cheng/article/details/8272388

上篇中实现的是Cocos2d-x提供给我们的动画,这次要实现的动画是自定义的动画。

这次实现的是让一个精灵一直执行我给的图片,从而达到动画的效果,还是截几张图看看。

图片资源来自解压《捕鱼达人》和《魔塔》

一共有18张图片。

[cpp] view plaincopyprint?
  1. CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png");
  2. CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,0,32,32));
  3. CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(32,0,32,32));
  4. CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(64,0,32,32));
  5. CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(96,0,32,32));
  6. CCArray *animFrames=CCArray::create();
  7. CC_BREAK_IF(!animFrames);
  8. animFrames->addObject(frame0);
  9. animFrames->addObject(frame1);
  10. animFrames->addObject(frame2);
  11. animFrames->addObject(frame3);
  12. CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f);
  13. CC_BREAK_IF(!animation);
  14. CCSprite *heroSprite0=CCSprite::createWithSpriteFrame(frame0);
  15. CC_BREAK_IF(!heroSprite0);
  16. heroSprite0->setPosition(ccp(100,100));
  17. addChild(heroSprite0,1);
  18. CCAnimate *animate=CCAnimate::create(animation);
  19. heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去


上面的动画是使英雄一直执行走动下去。

还有另外的一种方法来实现自定义动画:

[cpp] view plaincopyprint?
  1. CCAnimation* animation2 = CCAnimation::create();
  2. for(int i=1;i<19;i++)
  3. {
  4. char *tt=newchar[3];
  5. memset(tt,0,3);
  6. std::string s;
  7. if(i<10)
  8. {
  9. itoa(i,tt,10);
  10. s="fish00"+std::string(tt);
  11. }
  12. else
  13. {
  14. itoa(i,tt,10);
  15. s="fish0"+std::string(tt);
  16. }
  17. s=s+".png";
  18. CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage(s.c_str());
  19. animation2->addSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture, cocos2d::CCRectMake(0, 0, 100, 100)));
  20. delete []tt;
  21. }
  22. animation2->setDelayPerUnit(0.2f);
  23. CCAnimate* action = CCAnimate::create(animation2);
  24. CCTexture2D *playerRunTexture0 = CCTextureCache::sharedTextureCache()->addImage("fish001.png");
  25. CCSprite *p=CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture0, cocos2d::CCRectMake(0, 0, 100, 100)));
  26. p->setPosition(ccp(200,200));
  27. addChild(p,1);
  28. p->runAction(CCRepeatForever::create(action));


代码是如此的简单,我就不多说了。

最后照例贴出init函数的全部代码:

[cpp] view plaincopyprint?
  1. bool HelloWorld::init()
  2. {
  3. mPercentage=100;
  4. bool bRet = false;
  5. do
  6. {
  7. CC_BREAK_IF(! CCLayer::init());
  8. CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png");
  9. CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,0,32,32));
  10. CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(32,0,32,32));
  11. CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(64,0,32,32));
  12. CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(96,0,32,32));
  13. CCArray *animFrames=CCArray::create();
  14. CC_BREAK_IF(!animFrames);
  15. animFrames->addObject(frame0);
  16. animFrames->addObject(frame1);
  17. animFrames->addObject(frame2);
  18. animFrames->addObject(frame3);
  19. CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f);
  20. CC_BREAK_IF(!animation);
  21. CCSprite *heroSprite0=CCSprite::createWithSpriteFrame(frame0);
  22. CC_BREAK_IF(!heroSprite0);
  23. heroSprite0->setPosition(ccp(100,100));
  24. addChild(heroSprite0,1);
  25. CCAnimate *animate=CCAnimate::create(animation);
  26. heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去
  27. CCActionInterval *en=CCRotateBy::create(5,-360);
  28. CCSprite *hua=CCSprite::create("end.png");
  29. hua->setPosition(ccp(240,160));
  30. addChild(hua,1);
  31. hua->runAction(CCRepeatForever::create(en));
  32. CCAnimation* animation2 = CCAnimation::create();
  33. for(int i=1;i<19;i++)
  34. {
  35. char *tt=newchar[3];
  36. memset(tt,0,3);
  37. std::string s;
  38. if(i<10)
  39. {
  40. itoa(i,tt,10);
  41. s="fish00"+std::string(tt);
  42. }
  43. else
  44. {
  45. itoa(i,tt,10);
  46. s="fish0"+std::string(tt);
  47. }
  48. s=s+".png";
  49. CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage(s.c_str());
  50. animation2->addSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture, cocos2d::CCRectMake(0, 0, 100, 100)));
  51. delete []tt;
  52. }
  53. animation2->setDelayPerUnit(0.2f);
  54. CCAnimate* action = CCAnimate::create(animation2);
  55. CCTexture2D *playerRunTexture0 = CCTextureCache::sharedTextureCache()->addImage("fish001.png");
  56. CCSprite *p=CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture0, cocos2d::CCRectMake(0, 0, 100, 100)));
  57. p->setPosition(ccp(200,200));
  58. addChild(p,1);
  59. p->runAction(CCRepeatForever::create(action));
  60. bRet = true;
  61. } while (0);
  62. return bRet;
  63. }
原创粉丝点击