cocos2d-x基础 - 特效(2)
1. 动作混合
要处理多个动作混合的情况,就要用到Spawn类
具体用法:
label->runAction(Spawn::create(MoveBy::create(1, Point(30, 30)),
Repeat::create(RotateBy::create(1, 180), 3),
NULL));
上面让label同时进行移动和旋转的动作
2. 动作序列
动作序列就是将很多动作包装到一个序列里面,然后在一定的时间内自行完毕
创建一个动作序列使用Sequence类
例如让一个label先移动一定距离,再旋转一周,再反向旋转一周,然后反向移动一段距离
label->runAction(Sequence::create(MoveBy::create(1, Point(50, 50)),
RotateBy::create(1, 360),
MoveBy::create(1, Point(50, 50))->reverse(),
RotateBy::create(1, 360)->reverse(),
NULL));
实现效果如下:

3. 动作侦听
一个动作在执行结束后可以执行事件,使用CallFunc类来在序列结束和开始时添加一个侦听函数,里面放置要执行的操作
label->runAction(Sequence::create(CallFunc::create([](){
MessageBox("即将开始动作序列", "加油!");
}),
MoveBy::create(1, Point(50, 50)),
RotateBy::create(1, 360),
MoveBy::create(1, Point(50, 50))->reverse(),
RotateBy::create(1, 360)->reverse(),
CallFunc::create([](){
MessageBox("完成了动作序列", "恭喜!");
}),
NULL));
效果如图所示:

4. 逐帧动画
在程序中加入一个逐帧动画,首先需要一个动画素材

使用上面的专业软件就可以创建专业的逐帧动画,过程略
导出Sprite序列之后获得了两个文件:

.plist文件打开之后可以看到如图:

这个.plist文件保存了逐帧的信息
接下来加入如下代码:
// 创建帧动画帧的缓存
auto cache = SpriteFrameCache::getInstance();
cache->addSpriteFramesWithFile("anim.plist");
// 构造一个包含 SpriteFrame* 的vector
Vector<SpriteFrame*> vec;
char name[15]; // 创建一个代表frame名称的字符串
memset(name, 0, 15); // 清空name
for (int i=0; i<20; i++) {
sprintf(name, "anim%04d", i);
vec.pushBack(cache->getSpriteFrameByName(name)); // 将逐帧动画加入到vector中
}
// 创建动画
Animation *animation = Animation::createWithSpriteFrames(vec, 0.1f);
Animate *animate = Animate::create(animation);
// 创建一个精灵Sprite来播放动画
Sprite *s = Sprite::create();
addChild(s);
s->setPosition(visibleSize.width/2, visibleSize.height/2);
// 让s不断执行动画
s->runAction(RepeatForever::create(animate));
加入逐帧动画后的效果:
来源:CSDN
作者:CC_且听风吟
链接:https://blog.csdn.net/weixin_43826242/article/details/104133901