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,132*0,132,200));
SpriteFrame* frame2 = SpriteFrame::createWithTexture(textTure, Rect(132*2,132*0,132,200));
SpriteFrame* frame3 = SpriteFrame::createWithTexture(textTure, Rect(132*3,132*0,132,200));
SpriteFrame* frame4 = SpriteFrame::createWithTexture(textTure, Rect(132*0,132*1,132,200));
SpriteFrame* frame5 = SpriteFrame::createWithTexture(textTure, Rect(132*1,132*1,132,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,打包。保存地址就是project的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, "grossini_dance_%02d.png",i);//将精灵帧载入
auto frame = cache->getSpriteFrameByName(str);
arr1.pushBack(frame);
}
//运行动作
auto animation2 = Animation::createWithSpriteFrames(arr1,0.1f);
sp1->runAction(RepeatForever::create(Animate::create(animation2)));

4、效果图


最新文章

  1. linux文件系统体系结构 和 虚拟文件系统(VFS)
  2. angularjs-$http.post请求传递参数,后台Controller接受不到原因
  3. h5 notes
  4. 学用MVC4做网站六:后台管理(续)
  5. 微信支付:curl出错,错误码:60
  6. 建站随手记:about server stack
  7. JDBC简介及编码步骤
  8. ruby bundle config 镜像映射配置
  9. mysql设置连接超时时间参数:wait_timeout
  10. Android源码网站
  11. 【转】VB中应用DDE
  12. hdu 5029 Relief grain(树链剖分+线段树)
  13. 【锋利的JQuery-学习笔记】切换网页皮肤-且保存于Cookie
  14. 封装Unity3d的dll时的经验总结
  15. web工程调用hadoop集群1.2
  16. [Oracle] 11G自己主动收集统计信息
  17. tomcat的常用配置
  18. java中碰到无法解决的问题:无法访问类的getter访问器
  19. [LeetCode] Max Area of Island 岛的最大面积
  20. C语言实现快速翻转数组的顺序

热门文章

  1. elasticsearch模板 template
  2. Entity Framework Code First -- 延迟加载和预先加载
  3. 快速录入快递地址API接口实现
  4. MacOS 升级后pod 出现的问题
  5. 【SQL】IN、EXISTS和表连接三者的效率比较
  6. 多种效果进度指示层效果iOS源码项目
  7. OpenCv: 二维坐标的旋转方程
  8. 【sqli-labs】 less35 GET- Bypass Add Slashes(we dont need them) Integer based (GET型绕过addslashes() 函数的整型注入)
  9. 另一个维度:cocos-2d VS vue
  10. BOS工具之BOS应用框架