加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

cocos2d-x游戏开发(十六)帧动画

发布时间:2020-12-14 21:46:37 所属栏目:百科 来源:网络整理
导读:欢迎转载:http://www.jb51.cc/article/p-zngyomuh-zw.html 本来想写一下帧动画的,搜了一下网上好像有一大把,就懒得写了,直接贴代码。 [cpp] view plain copy print ? //精灵帧缓存 CCSpriteFrameCache*cache=CCSpriteFrameCache::sharedSpriteFrameCache

欢迎转载:http://www.52php.cn/article/p-zngyomuh-zw.html

本来想写一下帧动画的,搜了一下网上好像有一大把,就懒得写了,直接贴代码。

[cpp] view plain copy print ?
  1. //精灵帧缓存
  2. CCSpriteFrameCache*cache=CCSpriteFrameCache::sharedSpriteFrameCache();
  3. //添加plist文件到缓存
  4. cache->addSpriteFramesWithFile("lightning.plist");
  5. //创建数组用来存放帧序列
  6. CCArray*animFrames=CCArray::create();
  7. charstr[64]={0};
  8. for(inti=1;i<=18;++i){
  9. sprintf(str,"lightning%02d.png",i);
  10. CCSpriteFrame*frame=cache->spriteFrameByName(str);
  11. CCLOG(str);
  12. //添加帧到数组
  13. animFrames->addObject(frame);
  14. }
  15. //用帧序列生成帧动画信息,设定帧间隔时间
  16. CCAnimation*animation=CCAnimation::createWithSpriteFrames(animFrames,0.08);
  17. animation->setRestoreOriginalFrame(true);
  18. //动画缓存,取名字
  19. CCAnimationCache::sharedAnimationCache()->addAnimation(animation,"Lightning");
  20. //获取第一帧
  21. CCSpriteFrame*frame=CCSpriteFrameCache::sharedSpriteFrameCache()->spriteFrameByName("lightning01.png");
  22. //用第一帧作静态图
  23. CCSprite*first=CCSprite::createWithSpriteFrame(frame);
  24. first->setPosition(ccp(100,100));
  25. this->addChild(first);
  26. //用名字在缓存取帧动画信息
  27. CCAnimation*anim=CCAnimationCache::sharedAnimationCache()->animationByName("Lightning");
  28. //有动画信息生成动画
  29. CCAnimate*animate=CCAnimate::create(anim);
  30. //执行动画
  31. first->runAction(CCRepeatForever::create(animate));

本来还想做个gif的,mac下的那个软件太贵了,就算了。看个截图

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读