易妖游戏网
您的当前位置:首页Cocos2d-x 自定义动画

Cocos2d-x 自定义动画

来源:易妖游戏网
出处:

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

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

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

一共有18张图片。

[cpp]
       		CCTexture2D *pTexture=CCTextureCache::sharedTextureCache()->addImage("hero.png");
		CCSpriteFrame *frame0=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(0,0,32,32));
		CCSpriteFrame *frame1=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(32,0,32,32));
		CCSpriteFrame *frame2=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(,0,32,32));
		CCSpriteFrame *frame3=CCSpriteFrame::createWithTexture(pTexture,CCRectMake(96,0,32,32));
		CCArray  *animFrames=CCArray::create();
		CC_BREAK_IF(!animFrames);
		animFrames->addObject(frame0);
		animFrames->addObject(frame1);
		animFrames->addObject(frame2);
		animFrames->addObject(frame3);
		
		CCAnimation *animation=CCAnimation::createWithSpriteFrames(animFrames,0.2f);
		
		CC_BREAK_IF(!animation);

		CCSprite *heroSprite0=CCSprite::createWithSpriteFrame(frame0);
		CC_BREAK_IF(!heroSprite0);
		heroSprite0->setPosition((100,100));
		addChild(heroSprite0,1);
		CCAnimate *animate=CCAnimate::create(animation);
		heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去


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

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

[cpp]
  1. CCAnimation* animation2 = CCAnimation::create();
  2. for(int i=1;i<19;i++)
  3. {
  4. char *tt=new char[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((200,200));
  27. addChild(p,1);
  28. p->runAction(CCRepeatForever::create(action));
	CCAnimation* animation2 = CCAnimation::create(); 

	for(int i=1;i<19;i++)
	{
		char *tt=new char[3];
		memset(tt,0,3);
		std::string s;

		if(i<10)
		{
			itoa(i,tt,10);
			s="fish00"+std::string(tt);
		}
		else
		{
			itoa(i,tt,10);
			s="fish0"+std::string(tt);
		}

		s=s+".png";

	
		CCTexture2D *playerRunTexture = CCTextureCache::sharedTextureCache()->addImage(s.c_str()); 

		 animation2->addSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture, cocos2d::CCRectMake(0, 0, 100, 100)));  

		 delete []tt;
	
	}


	     
    
     
    animation2->setDelayPerUnit(0.2f);  
    CCAnimate* action = CCAnimate::create(animation2);  
    
	CCTexture2D *playerRunTexture0 = CCTextureCache::sharedTextureCache()->addImage("fish001.png"); 

	CCSprite *p=CCSprite::createWithSpriteFrame(CCSpriteFrame::createWithTexture(playerRunTexture0, cocos2d::CCRectMake(0, 0, 100, 100)));
	p->setPosition((200,200));
	addChild(p,1);

	p->runAction(CCRepeatForever::create(action));


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

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

[cpp]
  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(,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((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((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=new char[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((200,200));
  58. addChild(p,1);
  59. p->runAction(CCRepeatForever::create(action));
  60. bRet = true;
  61. } while (0);
  62. return bRet;
  63. }

因篇幅问题不能全部显示,请点此查看更多更全内容