cocos2d-x-2.x与3.x帧动画实现方式的改变

发表于2018-07-31
评论0 1.9k浏览
Cocos2d-x中,动画的具体内容是依靠精灵显示出来的,为了显示动态图片,我们需要不停切换精灵显示的内容,通过把静态的精灵变为动画播放器从而实现动画效果。随着cocos2dx-x3.x版本出现,与cocos2d-x-2.x相比在帧动画实现方式又会有所区别,为此本篇文章就给大家对比下这两个版本的帧动画实现方式。

直接上代码:  
2.x  
 //第二个参数表示显示区域的x,y,width,height
    frame0=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*0,0,237.5, 191));
    frame1=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*1,0,237.5, 191));
    frame2=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*2,0,237.5, 191));
    frame3=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*3,0,237.5, 191));
    Array *animFrames=Array::create();
    animFrames->addObject(frame0);
    animFrames->addObject(frame1);
    animFrames->addObject(frame2);
    animFrames->addObject(frame3);
    //根据4幅帧生成CCAnimation对象
	Animation *animation = Animation::createWithSpriteFrames(animFrames);
    //根据动画模板创建动画
    animation->setDelayPerUnit(0.2f);
    //animFrames->release();
    Animate *animate=Animate::create(animation);
3.x  
Vector<SpriteFrame*> frameVec;
	SpriteFrame* frame = NULL;
    //用纹理创建4幅帧动画
    SpriteFrame *frame0,*frame1,*frame2,*frame3;
    //第二个参数表示显示区域的x,y,width,height
    frame0=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*0,0,237.5, 191));
    frame1=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*1,0,237.5, 191));
    frame2=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*2,0,237.5, 191));
    frame3=SpriteFrame::createWithTexture(this->getTexture(),Rect(237.5*3,0,237.5, 191));
	frameVec.pushBack(frame0);
	frameVec.pushBack(frame1);
	frameVec.pushBack(frame2);
	frameVec.pushBack(frame3);
    //根据4幅帧生成CCAnimation对象
	Animation *animation = Animation::createWithSpriteFrames(frameVec);
    //根据动画模板创建动画
    animation->setDelayPerUnit(0.2f);
    //animFrames->release();
    Animate *animate=Animate::create(animation);
    return animate;

如社区发表内容存在侵权行为,您可以点击这里查看侵权投诉指引