【Cocos2d-x游戏引擎开发笔记(5)】自定义动画
2012-12-08 13:32
603 查看
原创文章,转载请注明出处:/article/1389517.html
上篇中实现的是Cocos2d-x提供给我们的动画,这次要实现的动画是自定义的动画。
这次实现的是让一个精灵一直执行我给的图片,从而达到动画的效果,还是截几张图看看。
图片资源来自解压《捕鱼达人》和《魔塔》
一共有18张图片。
上面的动画是使英雄一直执行走动下去。
还有另外的一种方法来实现自定义动画:
代码是如此的简单,我就不多说了。
最后照例贴出init函数的全部代码:
上篇中实现的是Cocos2d-x提供给我们的动画,这次要实现的动画是自定义的动画。
这次实现的是让一个精灵一直执行我给的图片,从而达到动画的效果,还是截几张图看看。
图片资源来自解压《捕鱼达人》和《魔塔》
一共有18张图片。
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(64,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(ccp(100,100)); addChild(heroSprite0,1); CCAnimate *animate=CCAnimate::create(animation); heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去
上面的动画是使英雄一直执行走动下去。
还有另外的一种方法来实现自定义动画:
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(ccp(200,200)); addChild(p,1); p->runAction(CCRepeatForever::create(action));
代码是如此的简单,我就不多说了。
最后照例贴出init函数的全部代码:
bool HelloWorld::init()
{
mPercentage=100;
bool bRet = false;
do
{
CC_BREAK_IF(! CCLayer::init());
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(64,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(ccp(100,100));
addChild(heroSprite0,1);
CCAnimate *animate=CCAnimate::create(animation);
heroSprite0->runAction(CCRepeatForever::create(animate));//一直执行下去
CCActionInterval *en=CCRotateBy::create(5,-360);
CCSprite *hua=CCSprite::create("end.png");
hua->setPosition(ccp(240,160));
addChild(hua,1);
hua->runAction(CCRepeatForever::create(en));
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(ccp(200,200)); addChild(p,1); p->runAction(CCRepeatForever::create(action));
bRet = true;
} while (0);
return bRet;
}
相关文章推荐
- 【Cocos2d-x游戏引擎开发笔记(5)】自定义动画
- 【Cocos2d-x游戏引擎开发笔记(4)】系统动画
- 【Cocos2d-x游戏引擎开发笔记(10)】自定义场景和场景跳转
- 【Cocos2d-x游戏引擎开发笔记(16)】动画速度的控制
- 【Cocos2d-x游戏引擎开发笔记(26)】中文和自定义字体
- 【Cocos2d-x游戏引擎开发笔记(26)】中文和自定义字体
- 【Cocos2d-x游戏引擎开发笔记(4)】系统动画
- 【Cocos2d-x游戏引擎开发笔记(10)】自定义场景和场景跳转
- 【Cocos2d-html5游戏引擎学习笔记(10)】自定义精灵动画
- 【Cocos2d-x游戏引擎开发笔记(16)】动画速度的控制
- 【Cocos2d-x游戏引擎开发笔记(26)】中文和自定义字体
- 【Cocos2d-x游戏引擎开发笔记(14)】Tiled Map Editor(二)
- 【Cocos2d-x游戏引擎开发笔记(9)】实现DotA的技能冷却效果
- 【Cocos2d-x游戏引擎开发笔记(6)】触屏事件与多媒体 (转)
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第九课: Cocos2D-X引擎框架4
- 【麦可网】Cocos2d-X跨平台游戏开发学习笔记---第二十四课:Cocos2D-X物理引擎之Box2D1-7
- 【Cocos2d-x游戏引擎开发笔记(6)】触屏事件与多媒体
- 【Cocos2d-x游戏引擎开发笔记(25)】XML解析
- 【Cocos2d-x游戏引擎开发笔记(1)】HelloWorld
- 【Cocos2d-x游戏引擎开发笔记(19)】简单数据存储以及抗锯齿