cocos2d-x 3.x 创建动画
2016-03-11 16:37
344 查看
首先创建动画步骤一般有3步:
1、创建一组SpriteFrame对象,每张动画图片为一个SpriteFrame对象。
2、用这组SpriteFrame对象创建一个Animation对象,该对象包含了动画所需的一些配置信息,比如每帧动画间隔时间、动画是播放一次还是永久播放。。。。。
3、利用Animation对象创建一个Animate对象,精灵直接调用runAction方法即可执行Animate动画。
一、没有打包的图片创建动画
auto sp = Sprite::create();
sp->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp);
//创建一个SpriteFrame列表
Vector<SpriteFrame*> sf;
int frameNum = 40;
//图片张数
SpriteFrame* frame = NULL;
//讲所有的SpriteFrame放到列表里
for (int i = 1; i <= frameNum;i++)
{
//用每一张图片创建一个SpriteFrame对象
frame = SpriteFrame::create(StringUtils::format("static/dahouzi00%d.png",i),
Rect(0,0,400,680));
//图片大小
sf.pushBack(frame);
}
//SpriteFrame列表创建动画对象
auto animation = Animation::createWithSpriteFrames(sf);
animation->setLoops(-1);
//-1表示循环播放,1表示播放一次。
animation->setDelayPerUnit(0.07);
//两张图片交替的间隔时间/速度
//将动画包装成一个动作
auto animate = Animate::create(animation);
//精灵播放动作
sp->runAction(animate);
二、用TexturePacker打包后的图片穿件动画
//将图片加载到缓存池
auto frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("xxx.plist", "xxx.png");
//创建精灵
auto sp = Sprite::create();
sp->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp);
//创建一个SpriteFrame列表
Vector<SpriteFrame*> sf;
int frameNum = 40;
//图片张数
SpriteFrame* frame = NULL;
//讲所有的SpriteFrame放到列表里
for (int i = 1; i <= frameNum; i++)
{
//从缓存池里获取一个SpriteFrame对象
frame = frameCache->getSpriteFrameByName(StringUtils::format("static/dahouzi00%d.png", i));
sf.pushBack(frame);
}
//SpriteFrame列表创建动画对象
auto animation = Animation::createWithSpriteFrames(sf);
animation->setLoops(-1);
//-1表示循环播放
animation->setDelayPerUnit(0.07);
//播放两张图片的间隔时间
//将动画包装成一个动作
auto animate = Animate::create(animation);
//精灵播放动作
sp->runAction(animate);
1、创建一组SpriteFrame对象,每张动画图片为一个SpriteFrame对象。
2、用这组SpriteFrame对象创建一个Animation对象,该对象包含了动画所需的一些配置信息,比如每帧动画间隔时间、动画是播放一次还是永久播放。。。。。
3、利用Animation对象创建一个Animate对象,精灵直接调用runAction方法即可执行Animate动画。
一、没有打包的图片创建动画
auto sp = Sprite::create();
sp->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp);
//创建一个SpriteFrame列表
Vector<SpriteFrame*> sf;
int frameNum = 40;
//图片张数
SpriteFrame* frame = NULL;
//讲所有的SpriteFrame放到列表里
for (int i = 1; i <= frameNum;i++)
{
//用每一张图片创建一个SpriteFrame对象
frame = SpriteFrame::create(StringUtils::format("static/dahouzi00%d.png",i),
Rect(0,0,400,680));
//图片大小
sf.pushBack(frame);
}
//SpriteFrame列表创建动画对象
auto animation = Animation::createWithSpriteFrames(sf);
animation->setLoops(-1);
//-1表示循环播放,1表示播放一次。
animation->setDelayPerUnit(0.07);
//两张图片交替的间隔时间/速度
//将动画包装成一个动作
auto animate = Animate::create(animation);
//精灵播放动作
sp->runAction(animate);
二、用TexturePacker打包后的图片穿件动画
//将图片加载到缓存池
auto frameCache = SpriteFrameCache::getInstance();
frameCache->addSpriteFramesWithFile("xxx.plist", "xxx.png");
//创建精灵
auto sp = Sprite::create();
sp->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(sp);
//创建一个SpriteFrame列表
Vector<SpriteFrame*> sf;
int frameNum = 40;
//图片张数
SpriteFrame* frame = NULL;
//讲所有的SpriteFrame放到列表里
for (int i = 1; i <= frameNum; i++)
{
//从缓存池里获取一个SpriteFrame对象
frame = frameCache->getSpriteFrameByName(StringUtils::format("static/dahouzi00%d.png", i));
sf.pushBack(frame);
}
//SpriteFrame列表创建动画对象
auto animation = Animation::createWithSpriteFrames(sf);
animation->setLoops(-1);
//-1表示循环播放
animation->setDelayPerUnit(0.07);
//播放两张图片的间隔时间
//将动画包装成一个动作
auto animate = Animate::create(animation);
//精灵播放动作
sp->runAction(animate);
相关文章推荐
- cocos2d-x手游性能优化总结
- TexturePacker打包后的文件在cocos2d-x加载和使用
- 学习手机游戏开发的两个方向 Cocos2d-x 和 Unity 3D/2D,哪个前景更好?
- 学Cocos2d-x,让移动游戏开发更简单
- CocosCreator的环境搭建连接android设备调试
- cocos2dx 逐帧动画
- 7、Cocos2dx 3.0游戏开发找小三之3.0版本号的代码风格
- quick-cocos2d-x数据存储之GameState
- cocos2d-x&nbsp;图片纹理优…
- Cocos2d-x自创双机游戏源码-任意对…
- IOS游戏开发之cocos2d-x实现自定义…
- IOS游戏开发之cocos2d-x实现手机振…
- 关于Cocos2d-x在新版xcode下横竖屏…
- Cocos2d-x 自己对某场景思路整理
- Cocos2d入门--1--初涉相关属性或代码
- Cocos2d入门--3--向量的应用
- Cocos2d入门--2--三角函数的应用
- Cocos2d-X-3.0之后的版本的环境搭建
- Cocos2d入门--3--小球运动
- Cocos2d入门--1--初涉相关属性或代码