1、FadeIn和FadeOut淡入淡出
记得设置精灵的Opacity为0 ,效果会更明显。如:landowner->setOpacity(0);
①// 创建一个FadeIn动作,执行该动作的节点会在5秒内淡入
auto fadeIn = FadeIn::create(5);
// 反转FadeIn动作,即节点会在5秒内淡出
auto fadeInReverse = fadeIn->reverse();
// 精灵按顺序执行fadeIn和fadeInReverse动作
apple1->runAction(Sequence::create(fadeIn,fadeInReverse, NULL));
② // 创建一个FadeOut动作,执行该动作的节点会在5秒内淡出
auto fadeOut = FadeOut::create(5);
// 反转FadeOut动作,即节点会在5秒内淡入
auto fadeOutReverse = fadeOut->reverse();
// 精灵执行FadeOut动作和fadeOutReverse动作
apple2->runAction(Sequence::create(fadeOut,fadeOutReverse, NULL));
2、JumpTo和JumpBy跳跃
①// 定义一个JumpTo动作
auto jumpTo = JumpTo::create(5.0, Vec2(860,320), 50, 2);
//精灵(球)运行JumpTo动作,将在5秒之内从原位置以2次跳跃动作移动到(860,320)位置
ball->runAction(jumpTo);
②// 定义一个JumpBy动作
auto jumpBy = JumpBy::create(5.0, Vec2(860,320), 50, 2);
//精灵(球)运行JumpBy动作,将在5秒之内从原位置以2次跳跃动作相对移动到(860,320)位置
ball->runAction(jumpBy);
3、MoveTo和MoveBy 移动
①// 创建一个MoveTo动作
auto moveTo = MoveTo::create(5, Vec2(860,320));
// 精灵执行MoveTo,将5秒内从当前位置移动到像素(860,320)的位置
plane->runAction(moveTo);
②// 定义一个MoveBy动作,
auto moveBy = MoveBy::create(5,Vec2(860,320));
// 精灵执行MoveBy,将在5秒内从原位置向右移动860像素,向上移动320像素
plane->runAction(moveBy);
4、RotateTo和RotateBy旋转
①/*创建一个精灵显示在左边,用于测试RotateTo*/
auto left = Sprite::create("dance.png");
left->setPosition(Vec2(visibleSize.width/2-300,visibleSize.height/2));
this->addChild(left);
// 定义第1个RotateTo动作,旋转到45°
auto rotateTo1 = RotateTo::create(2, 45);
// 定义第2个RotateTo动作,旋转到90°
auto rotateTo2 = RotateTo::create(2, 90);
// 顺序执行2个RotateTo动作,rotateTo1在原始位置基础上旋转45°
// rotateTo2在原始位置基础上旋转90°,执行完后精灵在原始位置基础上旋转90°。
left->runAction(Sequence::create(rotateTo1,rotateTo2, NULL));
②/*创建一个精灵显示在右边,用于测试RotateBy*/
auto right = Sprite::create("dance.png");
right->setPosition(Vec2(visibleSize.width/2+300,visibleSize.height/2));
this->addChild(right);
// 定义第1个RotateBy动作,旋转到45°
auto rotateBy1 = RotateBy::create(2, 45);
// 定义第2个RotateBy动作,旋转到90°
auto rotateBy2 = RotateBy::create(2, 90);
// 顺序执行2个RotateBy动作,rotateBy1在原始位置基础上旋转45°,
// rotateBy2在第一次旋转45°的基础上再旋转90°,执行完后精灵的最终旋转角度是45°+90°=135°。
right->runAction(Sequence::create(rotateBy1,rotateBy2, NULL));
5、ScaleTo和ScaleBy 放大缩小
①/*创建一个精灵将显示在左边,用于测试ScaleTo*/
auto left = Sprite::create("apple.png");
// 设置精灵位置.
left->setPosition(Vec2(visibleSize.width/2-300,visibleSize.height/2));
// 将精灵添加为当前层的子节点
this->addChild(left);
// 定义2个ScaleTo动作,每个动作都是将精灵放大2倍
auto scaleTo1 = ScaleTo::create(2, 2);
auto scaleTo2 = ScaleTo::create(2, 2);
// 顺序执行2个ScaleTo动作,scaleTo1放大到原始大小的2倍
// scaleTo2还是放大到原始大小的2倍,执行完后最终精灵放大到原始大小的2倍
left->runAction(Sequence::create(scaleTo1,scaleTo2, NULL));
②/*创建一个精灵将显示在左边,用于测试ScaleBy*/
auto right = Sprite::create("apple.png");
right->setPosition(Vec2(visibleSize.width/2+300,visibleSize.height/2));
this->addChild(right);
// 定义2个ScaleBy动作,每个动作都是将精灵放大2倍auto scaleBy1 = ScaleBy::create(2, 2);
auto scaleBy2 = ScaleBy::create(2, 2);
// 顺序执行2个ScaleTo动作,scaleBy1放大到当前大小(第一次是原始大小)的2倍
// scaleBy2放大到当前大小(已经放大过后)的2倍,执行完后最终精灵放大到原始大小的4倍
right->runAction(Sequence::create(scaleBy1,scaleBy2, NULL));
6、Show和Hide显示和隐藏
(记得先要把精灵的设置不可见 如: apple->setVisible(false);)
①// 创建一个Show动作
auto show = Show::create();
// 精灵执行show动作,精灵显示
apple->runAction(show);
②// 创建一个Hide动作
auto hide = Hide::create();
// 精灵执行hide动作,精灵将会隐藏
apple->runAction(hide);
7、ToggleVisibility显示到隐藏,隐藏到显示
// 创建一个ToggleVisibility动作
auto visibility = ToggleVisibility::create();
// ToggleVisibility动作切换节点的可见属性,则(苹果)精灵又变成可见。
apple->runAction(visibility);
8、Blink闪烁
// 创建一个Blink动作,执行该动作的节点会在2秒闪动3次
auto blink = Blink::create(2, 3);
// 精灵执行Blink动作
apple->runAction(blink);
9、TintTo和TintBy颜色渐变
①// 创建一个TintTo动作,执行该动作5秒内将节点着色渐变成红色
auto tintTo = TintTo::create(5, 255, 0, 0);
// 精灵执行TintTo动作
apple1->runAction(tintTo);
// 创建第二个精灵
auto apple2 = Sprite::create("apple.png");
// 设置精灵的位置
apple2->setPosition(Vec2(visibleSize.width/2+100, visibleSize.height/2));
// 将精灵添加为当前层的子节点
this->addChild(apple2);
②// 创建一个TintBy动作,执行该动作在一定时间内在节点的当前色彩值上加上相应的RGB色彩值
auto tintBy = TintBy::create(5, 0,-100,-100);
// 精灵执行TintBy动作
apple2->runAction(tintBy);
10、CallFunc函数的使用
①方法一
/**
定义了一个CallFunc动作,调用move函数,3.0后新的回调接口,由四个CC_CALLBACK取代。
其实CC_CALLBACK的差别就在于后面的数字
0就代表回调的是没有参数的函数,1就是有一个参数,2就是有两个参数,3就是有三个参数
*/
static int kTagPlane = 1;//设置精灵的tag值
//不带参数的
auto callFunc = CallFunc::create(CC_CALLBACK_0(CallFuncActionScene::move, this));
_plane->runAction(callFunc);
// 实现自定义的move函数
void CallFuncActionTest::move(){
// 通过tag值获得精灵对象
auto plane = this->getChildByTag(kTagPlane);
// 定义一个MoveTo动作,精灵从屏幕左边移动到屏幕右边
auto moveTo = MoveTo::create(3, Vec2(_screenWidth-plane->getContentSize().width/2, _screenHeight/2));
// 精灵(飞机)执行MoveTo动作
plane->runAction(moveTo);
}
//带参数的
auto callFunc1 = CallFuncN::create(CC_CALLBACK_1(CallFuncActionScene::move, this));
_plane->runAction(callFunc1 );
// 实现自定义的move函数
void CallFuncActionTest::move(Node* node){
// 通过tag值获得精灵对象
log(node->getTag());
auto plane = this->getChildByTag(kTagPlane);
// 定义一个MoveTo动作,精灵从屏幕左边移动到屏幕右边
auto moveTo = MoveTo::create(3, Vec2(_screenWidth-plane->getContentSize().width/2, _screenHeight/2));
// 精灵(飞机)执行MoveTo动作
plane->runAction(moveTo);
}
②方法二
// C++11的lambda表达式实现函数回调动作
auto callFunc = CallFunc::create([&]{
// 定义一个MoveTo动作,精灵从屏幕左边移动到屏幕右边
auto moveTo = MoveTo::create(3, Vec2(_screenWidth-_plane->getContentSize().width/2, _screenHeight/2));
// 精灵(飞机)执行MoveTo动作
_plane->runAction(moveTo);
});
// 精灵(飞机)执行CallFunc回调动作
_plane->runAction(callFunc);
11、callFuncN函数的使用
// 定义了一个CallFuncN动作,调用move函数,第三个参数是传递给move函数的参数(飞机精灵)
auto callFuncN = CallFuncN::create(CC_CALLBACK_1(CallFuncNActionTest::move, this,_plane));
// 精灵(飞机)执行CallFuncN动作
_plane->runAction(callFuncN);
// 实现自定义的move函数
void CallFuncNActionTest::move(Node* sender){//Sender表示的就是_plane
// 定义一个MoveTo动作
auto moveTo = MoveTo::create(3, Vec2(_screenWidth-sender->getContentSize().width/2, _screenHeight/2));
// 参数对象sender执行MoveTo动作,sender就是传递进来的Node对象(飞机精灵)
sender->runAction(moveTo);
}
12、Sequence、spawn动作组合的使用
①/*创建一个精灵显示在左边,用于测试Sequence组合动作*/
auto left = Sprite::create("dance.png");
left->setPosition(Vec2(visibleSize.width/2-200,visibleSize.height/2));
this->addChild(left);
// 定义第1个RotateTo动作,旋转45°
auto rotateTo1 = RotateTo::create(5, 45);
// 定义第2个RotateTo动作,旋转90°
auto rotateTo2 = RotateTo::create(5, 90);
// 注意:是“按顺序”执行2个RotateTo动作,即先执行完rotateTo1再执行rotateTo2,执行两个动作需要10秒
left->runAction(Sequence::create(rotateTo1,rotateTo2, NULL));
②/*创建一个精灵显示在右边,用于测试Sequence组合动作*/
auto right = Sprite::create("dance.png");
right->setPosition(Vec2(visibleSize.width/2+200,visibleSize.height/2));
this->addChild(right);
// 定义第1个RotateTo动作,旋转45°
auto rotateTo3 = RotateTo::create(5, 45);
// 定义第2个RotateTo动作,旋转90°
auto rotateTo4 = RotateTo::create(5, 90);
// 注意:是“同时”执行2个RotateTo动作,rotateTo3和rotateTo4同时执行,执行两个动作需要5秒
right->runAction(Spawn::create(rotateTo3,rotateTo4, NULL));
13、Repeat的使用
Repeat:
①// 定义一个闪烁动作
auto blink1 = Blink::create(2, 2);
②// 定义一个淡入动作
auto fadeIn1 = FadeIn::create(2);
③// 定义一个组合动作,按顺序执行淡入动作和闪烁动作
auto sequence1 = Sequence::create(blink1,fadeIn1, NULL);
④// 定义一个重复动作Repeate,执行3次上面定义的组合动作
auto repeat = Repeat::create(sequence1, 3);
// 精灵执行重复动作
left->runAction(repeat);
RepeatForever
①// 定义一个闪烁动作
auto blink2 = Blink::create(2, 2);
②// 定义一个淡入动作
auto fadeIn2 = FadeIn::create(2);
③// 定义一个组合动作,按顺序执行淡入动作和闪烁动作
auto sequence2 = Sequence::create(blink2,fadeIn2, NULL);
④// 定义一个重复动作RepeatForever,该动作将一直重复执行
auto repeatForever = RepeatForever::create(sequence2);
// 精灵执行重复动作
right->runAction(repeatForever);
14、DelayTime的使用
①// 定义一个MoveTo动作,2秒内从当前位置移动到屏幕最右边的位置
auto move = MoveTo::create(2, Vec2(visibleSize.width-plane->getContentSize().width/2, visibleSize.height/2));
②// 定义一个DelayTime动作,执行该动作时将产生5秒延迟
auto delay = DelayTime::create(5);
③// 定义一个Sequence动作,先执行DelayTime延迟动作,再执行MoveTo移动动作
auto sequence = Sequence::create(delay,move, NULL);
// 精灵(飞机)执行Sequence动作
plane->runAction(sequence);
15、Ease的使用
一、EaseIn、EaseOut、EaseInOut 由慢到快、由快到慢、先由慢到快,再由快到慢
①// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move1 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height/2));
// EaseIn动作,运行时由慢到快
auto easeIn = EaseIn::create(move1, 2);
// dance精灵执行EaseIn动作
dance->runAction(easeIn);
②// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move2 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*2));
// EaseOut动作,运行时由快到慢
auto easeOut = EaseOut::create(move2, 2);
// sister1精灵执行EaseOut动作
sister1->runAction(easeOut);
③// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move3 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*3.5));
// EaseInOut动作,运行时先由慢到快,再由快到慢
auto easeInOut = EaseInOut::create(move3, 2);
// sister2精灵执行EaseInOut动作
sister2->runAction(easeInOut);
二、EaseSineIn、EaseSineOut、EaseSineInOut 由慢到快、由快到慢、先由慢到快,再由快到慢
①// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move1 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height/2));
// EaseSineIn动作,运行时由慢到快
auto easeIn = EaseSineIn::create(move1, 2);
// dance精灵执行EaseSineIn动作
dance->runAction(easeIn);
②// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move2 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*2));
// EaseSineOut动作,运行时由快到慢
auto easeOut = EaseSineOut::create(move2, 2);
// sister1精灵执行EaseSineOut动作
sister1->runAction(easeOut);
③// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move3 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*3.5));
// EaseSineInOut动作,运行时先由慢到快,再由快到慢
auto easeInOut = EaseSineInOut::create(move3, 2);
// sister2精灵执行EaseSineInOut动作
sister2->runAction(easeInOut);
三、EaseExponentialIn、EaseExponentialOut、EaseExponentialInOut 由慢到快、由快到慢、先由慢到快,再由快到慢
①// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move1 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height/2));
// EaseExponentialIn动作,运行时由慢到快
auto easeIn = EaseExponentialIn::create(move1, 2);
// dance精灵执行EaseExponentialIn动作
dance->runAction(easeIn);
②// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move2 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*2));
// EaseExponentialOut动作,运行时由快到慢
auto easeOut = EaseExponentialOut::create(move2, 2);
// sister1精灵执行EaseExponentialOut动作
sister1->runAction(easeOut);
③// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move3 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*3.5));
// EaseExponentialInOut动作,运行时先由慢到快,再由快到慢
auto easeInOut = EaseExponentialInOut::create(move3, 2);
// sister2精灵执行EaseExponentialInOut动作
sister2->runAction(easeInOut);
四、EaseElasticIn、EaseElasticOut、EaseElasticInOut 由慢到快、由快到慢、先由慢到快,再由快到慢
①// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move1 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height/2));
// EaseElasticIn动作,运行时由慢到快
auto easeIn = EaseElasticIn::create(move1, 2);
// dance精灵执行EaseElasticIn动作
dance->runAction(easeIn);
②// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move2 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*2));
// EaseElasticOut动作,运行时由快到慢
auto easeOut = EaseElasticOut::create(move2, 2);
// sister1精灵执行EaseElasticOut动作
sister1->runAction(easeOut);
③// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move3 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*3.5));
// EaseElasticInOut动作,运行时先由慢到快,再由快到慢
auto easeInOut = EaseElasticInOut::create(move3, 2);
// sister2精灵执行EaseElasticInOut动作
sister2->runAction(easeInOut);
五、EaseBounceIn、EaseBounceOut、EaseBounceInOut 由慢到快、由快到慢、先由慢到快,再由快到慢
①// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move1 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height/2));
// EaseBounceIn动作,运行时由慢到快
auto easeIn = EaseBounceIn::create(move1, 2);
// dance精灵执行EaseBounceIn动作
dance->runAction(easeIn);
②// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move2 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*2));
// EaseBounceOut动作,运行时由快到慢
auto easeOut = EaseBounceOut::create(move2, 2);
// sister1精灵执行EaseBounceOut动作
sister1->runAction(easeOut);
③// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move3 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2,dance->getContentSize().height*3.5));
// EaseBounceInOut动作,运行时先由慢到快,再由快到慢
auto easeInOut = EaseBounceInOut::create(move3, 2);
// sister2精灵执行EaseBounceInOut动作
sister2->runAction(easeInOut);
16、Follow类的使用
①// 定义一个MoveBy动作
auto move = MoveBy::create(5, Vec2(visibleSize.width * 3, 0));
②// 定义一个和MoveBy动作相反的动作
auto move_back = move->reverse();
③// 定义一个组合动作
auto seq = Sequence::create(move, move_back, NULL);
④// 定义一个重复动作
auto rep = RepeatForever::create(seq);
// 精灵执行重复动作
dance->runAction(rep);
⑤// 当前层执行一个跟随动作
this->runAction(Follow::create(dance, Rect(0, 0, visibleSize.width * 2 - 100, visibleSize.height)));
17、Speed类的使用
①// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move1 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2, dance->getContentSize().height/2));
// 0.5f表示运行速度减半
auto action1 = Speed::create(move1, 0.5f);
// dance精灵执行action1动作
dance->runAction(action1);
②// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move2 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2, dance->getContentSize().height*2));
// 1.0f表示运行速度正常
auto action2 = Speed::create(move2, 1.0f);
// sister1精灵执行action2动作
sister1->runAction(action2);
③// 定义一个MoveTo动作,3秒移动到屏幕右边特定的位置
auto move3 = MoveTo::create(3, Vec2(visibleSize.width-dance->getContentSize().width/2, dance->getContentSize().height*3.5));
// 2.0f表示运行速度为正常速度的2倍
auto action3 = Speed::create(move3, 2.0f);
// sister2精灵执行action3动作
sister2->runAction(action3);
18、Animation的使用
// ①创建动画对象,它是将动画帧元素的集合对象,决定了动画帧的播放顺序以及时间间隔
auto animation = Animation::create();
// ②循环使用单张图片来创建动画帧
for( int i=46;i<55;i++)
{
// 将单张图片添加为精灵帧(即动画帧)
animation->addSpriteFrameWithFile(StringUtils::format("100%d.png",i));
}
// 设置动画播放的属性,3秒/15帧
animation->setDelayPerUnit(3.0f/15.0f);
// 让精灵对象在动画执行完后恢复到最初状态
animation->setRestoreOriginalFrame(true);
// ③创建动画动作
auto animate = Animate::create(animation);
// ④精灵对象(英雄)执行动画动作
hero->runAction(animate);
或者
Vector<SpriteFrame*> arrow;
for (int32_t i = 0; i < 46; i++)
{
auto spriteFrame = SpriteFrameCache::getInstance()->getSpriteFrameByName
(StringUtils::format("big_dlg_title (%d).png", i + 1));
arrow.pushBack(spriteFrame);
}
Sprite* pImgEffect = Sprite::createWithSpriteFrameName("big_dlg_title (1).png");
pImgEffect->setPosition(Vec2(pImgTitleBk->getContentSize() / 2.f) + Vec2(-4.f, 4.f));
pImgEffect->runAction(RepeatForever::create(Sequence::create(Show::create(),
Animate::create(Animation::createWithSpriteFrames(arrow, 0.08f)), DelayTime::create(2.f), Hide::create(), NULL)));
pImgTitleBk->addChild(pImgEffect, 1);
19、AnimationCache的使用
// ①获取精灵帧缓存的单例对象,并读取grossini.plist文件将精灵帧纹理添加到精灵帧缓存当中
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("grossini.plist");
// 创建精灵对象(舞蹈者)
auto dance = Sprite::create("grossini_dance_01.png");
// 设置精灵对象(舞蹈者)位置为屏幕正中
dance->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
// 把精灵对象(舞蹈者)添加为HelloWorldLayer层的子节点。
this->addChild(dance);
// ②创建动画帧缓冲并向缓冲区中添加动画文件
AnimationCache::getInstance()->addAnimationsWithFile("animations.plist");
// ③从缓冲区中获取文件中设定的名称为“dance_1”的动画对象。
auto animation = AnimationCache::getInstance()->getAnimation("dance_1");
// ④根据动画对象创建动画动作
auto animate = Animate::create(animation);
// 精灵对象(舞蹈者)执行动画动作
dance->runAction(animate);
20、AnimateBatch的使用
// ①获取精灵帧缓存的单例对象,并读取animation.plist文件将精灵帧纹理添加到精灵帧缓存当中
SpriteFrameCache::getInstance()->addSpriteFramesWithFile("animation.plist");
// ②使用精灵帧缓存中的精灵创建一个动画,并设置属性
auto animation = Animation::create();
// 循环从精灵帧缓存中获取与图片名称相对应的精灵帧组成动画
for(int i = 1;i<=8;i++){
std::string szName = StringUtils::format("z_00_0%d.png", i);
// 将单张图片添加为精灵帧(即动画帧) animation->addSpriteFrame(SpriteFrameCache::getInstance()->getSpriteFrameByName(szName));
}
// 设置动画播放的属性,3秒/15帧
animation->setDelayPerUnit(3.0f/15.0f);
// 让精灵对象在动画执行完后恢复到最初状态
animation->setRestoreOriginalFrame(true);
// ③用动画帧缓冲AnimationCache缓存所有动画和动画帧
AnimationCache::getInstance()->addAnimation(animation, "walk");
// ④创建背景精灵添加到当前层
auto bgSprite = Sprite::createWithSpriteFrameName("gamebg.png");
bgSprite->setPosition(Vec2(visibleSize.width/2,visibleSize.height/2));
this->addChild(bgSprite);
// ⑤创建僵尸精灵,设置坐标位置在屏幕之外,并添加到当前层
auto zSprite = Sprite::createWithSpriteFrameName("z_00_01.png"); zSprite->setPosition(Vec2(visibleSize.width+zSprite->getContentSize().width/2,visibleSize.height/2));
this->addChild(zSprite);
// ⑥从动画帧缓冲AnimationCache中获取之前缓存的动画
auto walk = AnimationCache::getInstance()->getAnimation("walk");
// ⑦创建动画动作
auto animate = Animate::create(walk);
// 创建一个重复动作,重复执行Animate动画动作
auto repeatanimate = RepeatForever::create(animate);
// 定义一个移动动作,让精灵对象从屏幕右边移动到屏幕左边
auto moveTo = MoveTo::create(10.f, Vec2(-zSprite->getContentSize().width/2, visibleSize.height/2));
// ⑧僵尸精灵执行重复动作(包含动画动作)和移动动作,执行效果是僵尸从屏幕的右边走到屏幕的左边
zSprite->runAction(repeatanimate);
zSprite->runAction(moveTo);
21、僵尸行走动画
// ①创建动画对象,它是将动画帧元素的集合对象,决定了动画帧的播放顺序以及时间间隔
auto animation = Animation::create();
// ②循环使用单张图片来创建动画帧
for( int i=1;i<8;i++)
{
// 将单张图片添加为精灵帧(即动画帧)
animation->addSpriteFrameWithFile(StringUtils::format("z_00_0%d.png",i));
}
// 设置动画播放的属性,3秒/15帧
animation->setDelayPerUnit(3.0f/15.0f);
// 让精灵对象在动画执行完后恢复到最初状态
animation->setRestoreOriginalFrame(true);
// ③创建动画动作
auto animate = Animate::create(animation);
// 创建一个重复动作,重复执行Animate动画动作
auto repeatanimate = RepeatForever::create(animate);
// 定义一个移动动作,让精灵对象从屏幕右边移动到屏幕左边
auto moveTo = MoveTo::create(10.f, Vec2(-zSprite->getContentSize().width/2, visibleSize.height/2));
// ④僵尸精灵执行重复动作(包含动画动作)和移动动作,执行效果是僵尸从屏幕的右边走到屏幕的左边
zSprite->runAction(repeatanimate);
zSprite->runAction(moveTo);