cocos2d-x 3.x之动画
动画由帧组成。在最简单的情况下,每一帧都是一个纹理。我们可以使用一个纹理序列来创建动画。然而显卡在绘图时,在纹理间的切换是一个巨大的开销,由于精灵可以显示部分纹理,因此通常更为高效的做法是把动画用到的多个纹理按照一定的顺序排列起来,然后放置在同一个纹理下。在创建动画时,我们不仅需要指定动画所使用的纹理,还需要指定每一帧所使用的是纹理的哪一部分。 auto animation = Animation::create(); for(int i = 0; i < 10; ++i) { char s[100] = {0}; sprintf(s,"%d.png",i); // 添加精灵帧到Animation实例 animation->addSpriteFrameWithFile(s); } // 设置每一帧持续时间,以秒为单位 animation->setDelayPerUnit(2.8f / 10.0f); // 设置是否在动画播放结束后恢复到第一帧 animation->setRestoreOriginalFrame(true); auto action = Animate::create(animation); auto sprite = Sprite::create(); sprite->setPosition(100,100); this->addChild(sprite); sprite->runAction(Sequence::create(action,action->reverse(),NULL)); // 缓存plist文件 SpriteFrameCache::getInstance()->addSpriteFramesWithFile("gameArts-hd.plist","gameArts-hd.png"); Vector<SpriteFrame *> spriteFrame; SpriteFrame *s1 = SpriteFrameCache::getInstance()->getSpriteFrameByName("hero_fly_1.png"); SpriteFrame *s2 = SpriteFrameCache::getInstance()->getSpriteFrameByName("hero_fly_2.png"); spriteFrame.pushBack(s1); spriteFrame.pushBack(s2); auto animation = Animation::createWithSpriteFrames(spriteFrame,0.2f); auto animate = Animate::create(animation); auto repeatAction = RepeatForever::create(animate); this->runAction(repeatAction); (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
- Electron+React+Webpack+Vscode应用桌面开发平台搭建
- c# – WebRequest无法正常下载大文件(?1 GB)
- c# – 正则表达式从字符串中提取img源代码
- reactjs – create-react-app和d3 – 为什么d3未定义?
- ios – 无法从以前的版本更改Bundle标识符
- ruby-on-rails – Rails content_for覆盖而不是追加
- cocos2d-x移植到android
- PostgreSQL字符日期和UNIX时间戳转换
- 在Swift中连接两个音频文件并播放它们
- reactjs – 在React Redux应用程序中使用OAuth2刷新令牌