【Cocos2d-x游戏引擎开发笔记(5)】自定义动画

来源:互联网 发布:php.ini设置 编辑:程序博客网 时间:2024/05/18 07:46
转载自:http://blog.csdn.net/zhy_cheng/article/details/8272388

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

 

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

 

 

 

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

一共有18张图片。

 

 

 

[cpp] view plaincopy
  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.   
  13. CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f);  
  14.   
  15. CC_BREAK_IF(!animation);  
  16.   
  17. CCSprite *heroSprite0=CCSprite::createWithSpriteFrame(frame0);  
  18. CC_BREAK_IF(!heroSprite0);  
  19. heroSprite0->setPosition(ccp(100,100));  
  20. addChild(heroSprite0,1);  
  21. CCAnimate *animate=CCAnimate::create(animation);  
  22. heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去  


 

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

 

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

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


 

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

 

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

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