Cocos2d-X使用CCAnimation创建动画

Cocos2d-X使用CCAnimation创建动画,第1张

概述http://blog.csdn.net/u010105970/article/details/40593059 动画在游戏中是非常常见的 程序1:创建一个简单的动画 首先需要在工程目录下的Resource文件夹中放一张有各种不同动作的图片 在程序中添加下面的代码 [cpp]  view plain copy #include "Animation.h"      CCScene* Animati

http://blog.csdn.net/u010105970/article/details/40593059

动画在游戏中是非常常见的


程序1:创建一个简单的动画

首先需要在工程目录下的Resource文件夹中放一张有各种不同动作的图片


在程序中添加下面的代码

[cpp] view plain copy #include"Animation.h" CCScene*Animation::scene() { CCScene*s=CCScene::create(); Animation*layer=Animation::create(); s->addChild(layer); returns; } boolAnimation::init() { cclayer::init(); CCSizewinSize=CCDirector::sharedDirector()->getWinSize(); //创建精灵 CCSprite*sp=CCSprite::create(); sp->setposition(ccp(winSize.wIDth/2,winSize.height/2)); addChild(sp); //精灵放大4倍 sp->setScale(4); //创建纹理 CCTexture2D*texture=CCTextureCache::sharedTextureCache()->addImage("walkleft.png"); //创建一个数组 CCArray*arr=CCArray::create(); //从纹理中扣了10帧frame,组成数组 for(inti=0;i<10;i++) //使用纹理创建精灵帧 //第一个参数:纹理 //第二个参数:矩形 CCSpriteFrame*frame=CCSpriteFrame::createWithTexture(texture, //第一个参数:矩形的x坐标 //第二个参数:矩形的y坐标 //第三个参数:矩形的宽度 //第四个参数:矩形的高度 CCRect(i*18,18,32)); arr->addobject(frame); } //使用精灵帧创建动画 //第一个参数:数组 //第二个参数:动画的帧数(播放两张图片的间隔时间) CCAnimation*animation=CCAnimation::createWithSpriteFrames(arr,0.1f); CCAnimate*animate=CCAnimate::create(animation); //播放动画 //CCRepeatForever::create动画播放无限次 sp->runAction(CCRepeatForever::create(animate)); returntrue; } @H_409_301@


执行结果:


程序2:有pList文件的动画加载

首先需要在工程目录下的Resource文件夹中放一张有各种不同动作的图片和一个pList文件

pList格式的文件实际上是一个XML文件,文件中的内容

<?xmlversion="1.0"enCoding="UTF-8"?> <!DOCTYPEpListPUBliC"-//AppleComputer//DTDPList1.0//EN""http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <pListversion="1.0"> <dict> <key>frames</key> <key>Boom_1.png</key> <key>frame</key> <string>{{204,305},{99,99}}</string> <key>offset</key> <string>{0,0}</string> <key>rotated</key> <false/> <key>sourcecolorRect</key> <string>{{0,0},108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>sourceSize</key> <string>{99,99}</string> </dict> <key>Boom_10.png</key> <dict> <key>frame</key> <string>{{103,307},99}}</string> <key>offset</key> <string>{0,0}</string> <key>rotated</key> <false/> <key>sourcecolorRect</key> <string>{{0,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>sourceSize</key> <string>{99,99}</string> </dict> <key>Boom_11.png</key> <string>{{103,206},248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_12.png</key> <string>{{105,2},108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_13.png</key> <key>Boom_14.png</key> <string>{{2,{101,101}}</string> <string>{101,101}</string> <key>Boom_15.png</key> <string>{{2,408},248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_16.png</key> <key>Boom_17.png</key> <key>Boom_18.png</key> <key>Boom_2.png</key> <key>Boom_3.png</key> <string>{{406,103},108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_4.png</key> <string>{{305,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_5.png</key> <string>{{204,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_6.png</key> <string>{{408,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_7.png</key> <string>{{307,108); List-style:decimal-leading-zero outsIDe; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_8.png</key> <string>{{206,248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> <key>Boom_9.png</key> <key>Metadata</key> <key>format</key> <integer>2</integer> <key>realTexturefilename</key> <string>PFBoom.png</string> <key>size</key> <string>{512,512}</string> <key>texturefilename</key> </pList> @H_409_301@

程序代码:

//有pList文件的动画加载
boolAnimation::init() cclayer::init(); CCSizewinSize=CCDirector::sharedDirector()->getWinSize(); //创建精灵
CCSprite*sp=CCSprite::create(); sp->setposition(ccp(winSize.wIDth/2,winSize.height/2)); addChild(sp); //精灵放大4倍
sp->setScale(4); //创建精灵帧缓存
CCSpriteFrameCache*frameCache=CCSpriteFrameCache::sharedSpriteFrameCache(); //创建数组
//添加PList文件
frameCache->addSpriteFramesWithfile("PFBoom.pList"); inti=1;i<=18;i++) charkey[128]; //通过名字获取精灵帧所在的图片 sprintf(key,"Boom_%d.png",i); //将图片添加到精灵帧中 CCSpriteFrame*frame=frameCache->spriteFrameByname(key); //将精灵帧添加到数组中 //使用数组创建动画 //动画消失 CCCallFunc*callfunc=CCCallFunc::create(sp,callfunc_selector(CCSprite::removeFromParent)); CCSequence*seq=CCSequence::create(animate,callfunc,NulL); sp->runAction(seq); true; } @H_409_301@

执行结果:


程序3:通过鼠标实现动画切换

首先创建一个Animation类

在AnimationPreload.h中添加下面代码

#ifndef__AnimationPreload_H__ #define__AnimationPreload_H__ #include"cocos2d.h" USING_NS_CC; classAnimationPreload:publiccclayer public: staticCCScene*scene(); boolinit(); CREATE_FUNC(AnimationPreload); boolcctouchBegan(CCtouch*,CCEvent*); CCSprite*sp; }; #endif @H_409_301@

在AnimationPreload.cpp中添加下面的代码

#include"AnimationPreload.h" CCScene*AnimationPreload::scene() AnimationPreload*layer=AnimationPreload::create(); boolAnimationPreload::init() //chushihu //获取窗口的大小 //(创建纹理)从纹理里扣了10帧frame,组成数组 CCArray*arr=CCArray::create();//retain inti=9;i>=0;i--) //创建精灵帧 CCSpriteFrame*frame=CCSpriteFrame::createWithTexture(texture, //将精灵帧添加到数组中 arr->addobject(frame); //通过数组创建动画 CCAnimation*animation=CCAnimation::createWithSpriteFrames(arr,0.05f); //加入到缓存 CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"MarioRun"); /***************************************************************/ /**有pList文件的动画加载**/ arr=CCArray::create(); //将pList文件添加到精灵帧缓存中 //创建动画 animation=CCAnimation::createWithSpriteFrames(arr,0); background-color:inherit">//加入到缓存 CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"PlaneBOOM"); sp=CCSprite::create(); //精灵方法4倍 //settouch settouchEnabled(true); settouchMode(kCCtouchesOneByOne); boolAnimationPreload::cctouchBegan(CCtouch*,CCEvent*) sp->stopAllActions(); staticinti=1; if(i==1) sp->runAction(CCAnimate::create( CCAnimationCache::sharedAnimationCache()->animationByname("PlaneBOOM") )); else CCAnimationCache::sharedAnimationCache()->animationByname("MarioRun") i=1-i; return0; } @H_409_301@


执行结果:

总结

以上是内存溢出为你收集整理的Cocos2d-X使用CCAnimation创建动画全部内容,希望文章能够帮你解决Cocos2d-X使用CCAnimation创建动画所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1057531.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-25
下一篇2022-05-25

发表评论

登录后才能评论

评论列表(0条)

    保存