【札记】cocos2d-x学习笔记:简单动画
需要说明的是:因为cocos2d-x是通用游戏引擎,为了保证兼容性和易用性,对动画机制作了最简单的设计(被做成了一个action)。但代价就是绘制动画的代码可能比较多,如果在实际开发中,一般都要选择自己封装。 这是我在IT在线教育平台麦子学院学习时的一篇笔记。希望能抛砖引玉。
这里只给出最基本的动画代码,具体使用要根据实际情况自己封装。最好自己开发一个编辑器。额外说一句,开发编辑器最好使用Qt,因为是跨平台的。
我们随便找一张动画资源图片,在66RPG里有很多。 <!--[if gte vml 1]><v:shapetype
id="_x0000_t75" coordsize="21600,21600" o:spt="75" o:preferrelative="t"
path="m@4@5l@4@11@9@11@9@5xe" filled="f" stroked="f">
<v:stroke joinstyle="miter"/>
<v:formulas>
<v:f eqn="if lineDrawn pixelLineWidth 0"/>
<v:f eqn="sum @0 1 0"/>
<v:f eqn="sum 0 0 @1"/>
<v:f eqn="prod @2 1 2"/>
<v:f eqn="prod @3 21600 pixelWidth"/>
<v:f eqn="prod @3 21600 pixelHeight"/>
<v:f eqn="sum @0 0 1"/>
<v:f eqn="prod @6 1 2"/>
<v:f eqn="prod @7 21600 pixelWidth"/>
<v:f eqn="sum @8 21600 0"/>
<v:f eqn="prod @7 21600 pixelHeight"/>
<v:f eqn="sum @10 21600 0"/>
</v:formulas>
<v:path o:extrusionok="f" gradientshapeok="t" o:connecttype="rect"/>
<o:lock v:ext="edit" aspectratio="t"/>
</v:shapetype><v:shape id="_x0000_i1025" type="#_x0000_t75" style='width:24pt;
height:24pt;mso-wrap-style:square;mso-position-horizontal-relative:page;
mso-position-vertical-relative:page'/><![endif]--><!--[if !vml]--><!--[endif]-->
#1 动画代码
<!--[if !supportLists]-->1 <!--[endif]-->CCSize s = CCDirector::sharedDirector()->getWinSize();
<!--[if !supportLists]-->2 <!--[endif]-->
<!--[if !supportLists]-->3 <!--[endif]-->//#1:生成动画需要的数据类
<!--[if !supportLists]-->4 <!--[endif]-->CCTexture2D *texture=CCTextureCache::sharedTextureCache()->addImage("pic2476.png");
<!--[if !supportLists]-->5 <!--[endif]-->CCSpriteFrame *frame0=CCSpriteFrame::frameWithTexture(texture,CCRectMake(32*0, 48*0, 32, 48));
<!--[if !supportLists]-->6 <!--[endif]-->CCSpriteFrame *frame1=CCSpriteFrame::frameWithTexture(texture,CCRectMake(32*1, 48*0, 32, 48));
<!--[if !supportLists]-->7 <!--[endif]-->CCSpriteFrame *frame2=CCSpriteFrame::frameWithTexture(texture,CCRectMake(32*2, 48*0, 32, 48));
<!--[if !supportLists]-->8 <!--[endif]-->CCSpriteFrame *frame3=CCSpriteFrame::frameWithTexture(texture,CCRectMake(32*3, 48*0, 32, 48));
<!--[if !supportLists]-->9 <!--[endif]-->
<!--[if !supportLists]-->10 <!--[endif]-->CCMutableArray<CCSpriteFrame*> *animFrames = new CCMutableArray<CCSpriteFrame*>(4);
<!--[if !supportLists]-->11 <!--[endif]-->animFrames->addObject(frame0);
<!--[if !supportLists]-->12 <!--[endif]-->animFrames->addObject(frame1);
<!--[if !supportLists]-->13 <!--[endif]-->animFrames->addObject(frame2);
<!--[if !supportLists]-->14 <!--[endif]-->animFrames->addObject(frame3);
<!--[if !supportLists]-->15 <!--[endif]-->
<!--[if !supportLists]-->16 <!--[endif]-->CCAnimation *animation = CCAnimation::animationWithFrames(animFrames, 0.2f);
<!--[if !supportLists]-->17 <!--[endif]-->animFrames->release();
<!--[if !supportLists]-->18 <!--[endif]-->//#2:初始化并设置Sprite
<!--[if !supportLists]-->19 <!--[endif]-->CCSprite *sprite = CCSprite::spriteWithSpriteFrame(frame0);//设置一个初始frame
<!--[if !supportLists]-->20 <!--[endif]-->sprite->setPosition( ccp( s.width/2, s.height/2) );
<!--[if !supportLists]-->21 <!--[endif]-->addChild(sprite);
<!--[if !supportLists]-->22 <!--[endif]-->
<!--[if !supportLists]-->23 <!--[endif]-->//#3:使用animation生成一个动画动作animate
<!--[if !supportLists]-->24 <!--[endif]-->CCAnimate *animate = CCAnimate::actionWithAnimation(animation, false);
<!--[if !supportLists]-->25 <!--[endif]-->sprite->runAction(CCRepeatForever::actionWithAction(animate));//重复播放
注意,cocos2dx不支持使用clip的动画,另外,clip动画的开发成本很高,在智能手机这种大内存的平台是否适用(牺牲内存换开发速度么?),值得商量。
#2 相关的类关系图
简单过程是,使用CCTexture2D加载图片 ,用CCTexture2D生成对应的CCSpriteFrame(对应的就是帧),将CCSpriteFrame添加到CCAnimation生成动画数据,用CCAnimation生成CCAnimate(就是最终的动画动作),最后用CCSprite执行这个动作。
<!--[if !supportLineBreakNewLine]-->
<!--[endif]-->