cocos2dx3.1 texturepacker播放动画
来源:互联网 发布:unity3d手游大全 编辑:程序博客网 时间:2024/06/09 08:00
先使用texturepacker把所需要使用的帧动画打包成一张图片和一个plist文件。使用大图的好处就是可以一次性载入图片,然后通过plist文件确定图片的位置,在内存中寻找图片数据,这就减少了I/O操作,使效率大大提高。但是相对来说图片的大小变大了,因为中间多了很多的空白的地方。这就是算法分析中时间与空间的矛盾吧。
然后使用如下的代码播放动画:
//获得精灵帧的实例,并通过plist文件载入精灵帧
SpriteFrameCache* cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("bear.plist");
//把所有的精灵帧放入Vector数组中
Vector<SpriteFrame*> frameArray;
for (int i = 0; i < 3; i++)
{
string name("bear");
char text[8];
sprintf(text,"%d",i);
string temp(text);
name += temp + ".png";
SpriteFrame* frame = cache->getSpriteFrameByName(name);
frameArray.pushBack(frame);
}
//定义第一个精灵,后续使用这个精灵播放动画
Sprite* sprite = Sprite::createWithSpriteFrameName("bear0.png");
sprite->setPosition(ccp(400,200));
this->addChild(sprite);
//从数组中获得精灵帧,并获得Animation的实例,后面的两个参数分别是每一次循环的间隔时间,和循环的次数
Animation* animation = Animation::createWithSpriteFrames(frameArray,0.2f);
//播放动作
sprite->runAction(RepeatForever::create(Animate::create(animation)));
使用最新的quick cocos的lua代码如下:(原理是一样的)
function MainScene:runAnimation()
-- body
print("runAnimation()")
local cache = cc.SpriteFrameCache:getInstance()
if cache ~= nil then
--todo
print("cache is null")
end
cache:addSpriteFrames("bear.plist")
print("addspriteframe")
local array = {}
for i = 1,3 do
local str = "bear" .. tostring(i-1) .. ".png"
array[i] = cc.SpriteFrameCache:getInstance():getSpriteFrame(str)
end
local sprite = cc.Sprite:createWithSpriteFrame(array[3])
sprite:setPosition(500,200)
self:addChild(sprite)
local animation = cc.Animation:createWithSpriteFrames(array,0.2)
sprite:runAction(cc.RepeatForever:create(cc.Animate:create(animation)))
end
- cocos2dx3.1 texturepacker播放动画
- cocos2dx3.1精灵反向动画(随即跳跃)
- TexturePacker生成动画脚本
- cocos2dx3.2 spine动画
- cocostudio1.6UI编辑器中动画使用 cocos2dx3.3 播放问题记录
- cocos2dx3.1.1如何播放声音
- cocos2dx 边缘1像素的图片/帧动画,用Texturepacker打包后,移动动作抖动
- 《初学cocos2d-x》- 动画TexturePacker(3)
- cocos2dx中的动画以及TexturePacker使用
- [学习笔记] 用TexturePacker建立动画Animation
- cocos2dx中的动画以及TexturePacker使用
- cocos2dx3.2 判断音效是否播放
- TexturePacker
- TexturePacker
- texturepacker
- TexturePacker
- TexturePacker
- texturepacker
- 浅谈我对Java异常类的了解
- 光电整纬机(日本世联电子株式会社)
- 将WordPress作者存档链接中的用户名改为用户ID
- 05.编辑模式-删除
- 周总结
- cocos2dx3.1 texturepacker播放动画
- HDU1678 Shopaholic【水题】
- 颠倒数组中的所有元素
- WordPress 修改自定义文章类型的固定链接结构
- 杂谈
- jquery validate使用和扩展规则,jqueryvalidate
- 吝啬的国度 nyoj
- HDU1708 Fibonacci String【水题】
- C# 调用C++ DLL 总结