【Cocos2d-x游戏引擎开发笔记(5)】自定义动画
来源:互联网 发布:php.ini设置 编辑:程序博客网 时间:2024/05/18 07:46
转载自:http://blog.csdn.net/zhy_cheng/article/details/8272388
上篇中实现的是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;
- }
0 0
- 【Cocos2d-x游戏引擎开发笔记(5)】自定义动画
- 【Cocos2d-x游戏引擎开发笔记(5)】自定义动画
- 【Cocos2d-x游戏引擎开发笔记(4)】系统动画
- 【Cocos2d-x游戏引擎开发笔记(4)】系统动画
- 【Cocos2d-x游戏引擎开发笔记】定时器
- 【Cocos2d-x游戏引擎开发笔记(16)】动画速度的控制
- 【Cocos2d-x游戏引擎开发笔记(16)】动画速度的控制
- 【Cocos2d-html5游戏引擎学习笔记(10)】自定义精灵动画
- 【Cocos2d-x游戏引擎开发笔记(10)】自定义场景和场景跳转
- 【Cocos2d-x游戏引擎开发笔记(26)】中文和自定义字体
- 【Cocos2d-x游戏引擎开发笔记(26)】中文和自定义字体
- 【Cocos2d-x游戏引擎开发笔记(10)】自定义场景和场景跳转
- 【Cocos2d-x游戏引擎开发笔记(26)】中文和自定义字体
- 【Cocos2d-x游戏引擎开发笔记(1)】HelloWorld
- 【Cocos2d-x游戏引擎开发笔记(7)】定时器
- 【Cocos2d-x游戏引擎开发笔记(18)】图形绘制
- 【Cocos2d-x游戏引擎开发笔记(20)】SQLite专题
- 【Cocos2d-x游戏引擎开发笔记(21)】CCHttpRequest联网
- WPF中利用ODBC连接读取MYSQL中的数据
- iOS开发之静态库的制作
- Oracle 存储结构详解
- BestCoder Round #17
- 金融行业移动信息化解决方案
- 【Cocos2d-x游戏引擎开发笔记(5)】自定义动画
- java.lang.VerifyError解决方案 Android
- Cas(03)——Cas Server中各配置文件介绍
- 能够把自动问答机制应用到运维中吗?
- cocos2dx 3.1 cpp版本转lua版本 mac
- SQLCookbook 学习笔记 6 字符串
- ios 一步一步学会自定义地图吹出框(CalloutView)-->(百度地图,高德地图,google地图)
- iOS 修改状态栏字的颜色(白色)
- Java程序员从笨鸟到菜鸟之(十六)CSS基础积累总结(上)