Cocos2d-x3.3beta0创建动画的3种方式

Cocos2d-x3.3beta0创建动画的3种方式,第1张

概述1、单独加载精灵对象 渲染效率低,浪费资源,不推荐用该方法。代码如下:注:代码只需贴到HelloWorldScene.cpp中即可。 //First,单独渲染每一个精灵帧 auto sprite = Sprite::create("grossini_dance_01.png"); sprite->setPosition(Vec2(visibleSize.width/2,visibl 1、单独加载精灵对象
渲染效率低,浪费资源,不推荐用该方法。代码如下:注:代码只需贴到HelloWorldScene.cpp中即可。
//First,单独渲染每一个精灵帧    auto sprite = Sprite::create("grossini_dance_01.png");    sprite->setposition(Vec2(visibleSize.wIDth/2,visibleSize.height/4*3));    addChild(sprite);        auto animation = Animation::create();    char strname[50] = {0};    for(int i = 1; i <= 14; i++)    {        sprintf(strname,"grossini_dance_%02d.png",i);        animation->addSpriteFrameWithfile(strname);//将所有的精灵加载到animation    }        animation->setDelayPerUnit(3.0f / 14);//3秒内播放14帧动画    animation->setRestoreOriginalFrame(true);//重头开始播放    sprite->runAction(RepeatForever::create(Animate::create(animation)));//执行动作
2、一次加载,使用精灵帧
效率和资源有提高,但是使用Rect截取精灵对象不方便。代码如下:
//Second,一次渲染    auto textTure = Director::getInstance()->getTextureCache()->addImage("dragon_animation.png");    SpriteFrame* frame0 = SpriteFrame::createWithTexture(textTure,Rect(132*0,132*0,132,200));//截取精灵帧    SpriteFrame* frame1 = SpriteFrame::createWithTexture(textTure,Rect(132*1,200));    SpriteFrame* frame2 = SpriteFrame::createWithTexture(textTure,Rect(132*2,200));    SpriteFrame* frame3 = SpriteFrame::createWithTexture(textTure,Rect(132*3,200));    SpriteFrame* frame4 = SpriteFrame::createWithTexture(textTure,132*1,200));    SpriteFrame* frame5 = SpriteFrame::createWithTexture(textTure,200));        Vector<SpriteFrame*> arr;//加载精灵帧    arr.pushBack(frame0);    arr.pushBack(frame1);    arr.pushBack(frame2);    arr.pushBack(frame3);    arr.pushBack(frame4);    arr.pushBack(frame5);    auto sp = Sprite::createWithSpriteFrame(frame0);//用第一帧精灵对象,初始化精灵    sp->setposition(Vec2(visibleSize.wIDth/2,visibleSize.height/2));    addChild(sp);        auto animation1 = Animation::createWithSpriteFrames(arr,0.2f);//执行动作    sp->runAction(RepeatForever::create(Animate::create(animation1)));
3、使用TexturePacker打包精灵对象,帧加载
推荐使用该方法:1)打开TexturePacker工具,addSprite导入精灵对象;2)Data Format选择cocos2d;3)Texture format使用
PNG格式,Layout的Max Size W和H可以改动。但是尺寸是2的幂。4)Publish sprite sheet,打包,保存地址就是工程的Resource即可,代码如下:
//Third,first和second的集合,使用TexturePacker工具,将精灵对象打包    auto cache = SpriteFrameCache::getInstance();    cache->addSpriteFramesWithfile("animation.pList");//加载pList文件        auto sp1 = Sprite::createWithSpriteFramename("grossini_dance_01.png");//使用第一帧精灵初始化对象,精灵对象的名字与pList中的名字一致    sp1->setposition(Vec2(visibleSize.wIDth/2,visibleSize.height/4));    addChild(sp1);        Vector<SpriteFrame*> arr1;    char str[50] = {0};    for(int i = 1; i <= 14; i++)    {        sprintf(str,i);//将精灵帧加载        auto frame = cache->getSpriteFrameByname(str);        arr1.pushBack(frame);    }    //执行动作    auto animation2 = Animation::createWithSpriteFrames(arr1,0.1f);    sp1->runAction(RepeatForever::create(Animate::create(animation2)));

4、效果图 总结

以上是内存溢出为你收集整理的Cocos2d-x3.3beta0创建动画的3种方式全部内容,希望文章能够帮你解决Cocos2d-x3.3beta0创建动画的3种方式所遇到的程序开发问题。

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

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

原文地址: https://outofmemory.cn/web/1002510.html

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

发表评论

登录后才能评论

评论列表(0条)

保存