~~~~我的生活,我的点点滴滴!!
我们简单看看Action的继承树状图:
动作类(Action)是所有动作的基类,它创建的一个对象代表一个动作。动作作用于Node,因此每个动作都需要由Node对象执行。动作类(Action)作为基类,实际上是一个接口,动作类的大多数实现类都派生于有限时间动作类(FiniteTimeAction)。
但是在实际开发中我们通常用到两类动作-即时动作和持续动作,它们均继承于有限时间动作类。
一、即时动作
即时动作是能够立刻完成的动作,这类动作是在下一帧立刻完成的动作,如设定位置、设定缩放等。把它们包装成动作后,可以与其他动作类组合为复杂动作。
下面介绍一些常用即时动作
1、Place
该动作用于将节点放置到某个指定位置,其作用与修改节点的position属性相同。例如,将节点放到屏幕坐标(50, 200)处的代码如下:
//在Point(50,200)处放一个node
auto placeAction = Place::create(Point(50,200));
auto sprite_1 = Sprite::create("role1.png");
//先添加
this->addChild(sprite_1);
//类似于setPosition功能
sprite_1->runAction(placeAction);相当于把这个精灵放到 (50,200)处。
2、FlipX和FlipY
这两个动作分别用于将精灵沿X轴和Y轴反向显示,其作用与设置精灵的FlipX和FlipY属性相同,将其封装成动作接口是为了便于与其他动作进行组合。
下面代码将一个精灵移动到一端后反向显示再进行移回的动作:
//开启翻转
auto flipxAction = FlipX::create(true);
//移到Point(400,200),使用2s
auto moveToActionGo = MoveTo::create(2.0f, Point(300,500));
//移到Point(50,50),使用2s
auto moveToActionBack = MoveTo::create(2.0f, Point(50,50));
//Sequence是动作序列,以NULL表示参数传入结束
auto action = Sequence::create(moveToActionGo, flipxAction, moveToActionBack, NULL);
auto sprite_2 = Sprite::create("role1.png");
this->addChild(sprite_2);
sprite_2->runAction(action);
3、Show和Hide
这两个动作分别用于显示和隐藏节点,其作用与设置节点的visible属性作用一样。例如:为了使精灵完成移动后隐藏起来。
代码如下:
//隐藏
auto hideAction = Hide::create();
//显示
auto showAction = Show::create();
//移到Point(100,100),使用1s
auto moveToAction_1 = MoveTo::create(1.0f, Point(100,100));
//移到Point(200,200),使用1s
auto moveToAction_2 = MoveTo::create(1.0f, Point(200,200));
//移到Point(300,300),使用1s
auto moveToAction_3 = MoveTo::create(1.0f, Point(300,300));
//Sequence是动作序列,以NULL表示参数传入结束, 移到_1后隐藏,移动_2后显示
auto action = Sequence::create(moveToAction_1, hideAction, moveToAction_2, showAction, moveToAction_3, NULL);
auto sprite_3 = Sprite::create("role1.png");
this->addChild(sprite_3);4、CallFunc与CallFuncN
在cocos2dx 3.x版本后CallFunc系列动作只包括CallFunc、CallFuncN两个动作,CallFuncN需要添加一个node节点作为参数,传递给调用函数,他们都是用来在动作中进行方法调用,如在游戏中为了节约内存资源,
我们可以在动作完成后调用相应函数清理内存等一系列操作。
在2.x版本中的 CallFuncND 和 CallFuncO 都可以通过 CallFunc 和 CallFuncN 进行实现。
4.1 CallFunc
/** creates the action with the callback of type std::function<void()>.
This is the preferred way to create the callback.
* When this funtion bound in js or lua ,the input param will be changed
* In js: var create(var func, var this, var [data]) or var create(var func)
* In lua:local create(local funcID)
*/
static CallFunc * create(const std::function<void()>& func);看完声明后,我们看看几种CallFunc使用的不同写法
//写法1
auto action1 = CallFunc::create( CC_CALLBACK_0(MyClass::callback_0,this));
auto action2 = CallFunc::create( CC_CALLBACK_0(MyClass::callback_1,this, additional_parameters));
//写法2
auto action1 = CallFunc::create( std::bind( &MyClass::callback_0, this));
auto action2 = CallFunc::create( std::bind( &MyClass::callback_1, this, additional_parameters));
//lambdas表达式写法
auto action1 = CallFunc::create(
[&](){
auto s = Director::getInstance()->getWinSize();
auto label = LabelTTF::create("called:lambda callback", "Marker Felt", 16);
label->setPosition(ccp( s.width/4*1,s.height/2-40));
this->addChild(label);
} );4.2 CallFuncN
/** creates the action with the callback of type std::function<void()>.
This is the preferred way to create the callback.
*/
static CallFuncN * create(const std::function<void(Node*)>& func);注意到该回调动作带有一个Node*参数。
假设回调函数:
void HelloWorld::callback(Node* sender, int num); //写法1
auto action = Sequence::create(
MoveBy::create(2.0f, Point(150,0)),
CallFuncN::create( CC_CALLBACK_1(ActionCallFuncN::callback, this)),
NULL);
//写法2
auto action = Sequence::create(
MoveBy::create(2.0f, Point(150,0)),
CallFuncN::create(std::bind(&ActionCallFuncN::callback,this,std::placeholders::_1)),
NULL);
//写法3
auto action = Sequence::create(
MoveBy::create(2.0f, Point(150,0)),
CallFuncN::create([&](Node* sender){
//回调动作代码
//dosomething
}),
NULL);受益于C++11的新语法特性 std::bind ; CallFuncND 和 CallFuncO 都可以通过 CallFunc 和 CallFuncN 进行实现。
4.3、CallFuncND
回调动作中带有一个Node*参数和一个void*参数。
实现过程类似于 CallFuncN
假设回调函数是 :
void HelloWorld::doRemoveFromParentAndCleanup(Node* sender, bool cleanup);
那么在回调动作中:
CallFuncN::create( CC_CALLBACK_1(HelloWorld::doRemoveFromParentAndCleanup, this, true));
这样就实现了等价于 CallFuncND 的回调动作。
4.4、CallFuncO
回调动作中带有一个Object*参数。
实现过程类似于 CallFunc
假设回调函数是: void HelloWorld::callback(Node* node, bool cleanup);
那么在回调动作中:
_grossini 为那个object对象
CallFunc::create( CC_CALLBACK_0(HelloWorld::callback, this, _grossini, true)
这样就实现了等价于 CallFuncO 的回调动作。
二、持续动作
1、MoveTo和MoveBy
//移动到Position的地方
MoveTo::create(float duration, const Point& position);
//移动的改变值为Position
MoveBy::create(float duration, const Point& position);很多时候他两的效果相似。
2、JumpTo和JumpBy
JumpTo::create(float duration, const Point& position, float height, int jumps);
JumpBy::create(float duration, const Point& position, float height, int jumps);//以抛物线方式跳到Point(400,50),最大高度200,跳2次跳到
auto jumpTo = JumpTo::create(3.0f, Point(400,50), 200, 2);
auto actionTo = Sequence::create(jumpTo, NULL);
auto sprite_5 = Sprite::create("role1.png");
this->addChild(sprite_5);
sprite_5->setPosition(Point(50,50));
sprite_5->runAction(actionTo);
////以抛物线方式跳动Point(400,50),最大高度200,跳4次跳到
auto jumpBy = JumpBy::create(4.0f, Point(400,50), 200, 4);
auto actionBy = Sequence::create(jumpBy, NULL);
auto sprite_6 = Sprite::create("role1.png");
this->addChild(sprite_6);
sprite_6->setPosition(Point(50,50));
sprite_6->runAction(actionBy);3、BezierTo和BezierBy
//先声明一个贝尔结构体
ccBezierConfig bezier;
//设置目标位置
bezier.endPosition = Point(50, 400);
//设置控制点1
bezier.controlPoint_1 = Point(0,0);
//设置控制点2
bezier.controlPoint_2 = Point(200, 50);
auto bezierTo = BezierTo::create(2.0f, bezier);
auto sprite_7 = Sprite::create("role1.png");
this->addChild(sprite_7);
sprite_7->runAction(bezierTo);BezierBy基本与BezierTo一样写法
4、ScaleTo和ScaleBy
//缩放时间,缩放比例
ScaleTo::create(float duration, float s);
ScaleBy::create(float duration, float s);
//原地缩放
auto scaleTo = ScaleTo::create(4.0f, 2);
auto actionTo = Sequence::create(scaleTo, NULL);
auto sprite_8 = Sprite::create("role1.png");
this->setPosition(Point(200,200));
this->addChild(sprite_8);
sprite_8->runAction(actionTo);5、RotateTo和RotateBy
RotateTo::create(float duration, float deltaAngle);
RotateBy::create(float duration, float deltaAngle);先让其缩放,然后在旋转:
//原地缩放2倍
auto scaleTo = ScaleTo::create(4.0f, 2);
//原地旋转90度
auto rotateTo = RotateTo::create(2.0f, 90.0f);
auto actionTo = Sequence::create(scaleTo, rotateTo, NULL);
auto sprite_8 = Sprite::create("role1.png");
this->setPosition(Point(200,200));
this->addChild(sprite_8);
sprite_8->runAction(actionTo);三、视觉特效动作
1、FadeIn, FadeOut和FateTo
//淡入
FadeIn::create(float d);
//淡出
FadeOut::create(float d);
//一定时间内透明度变化
FadeTo::create(float duration, GLubyte opacity);
//原地缩放2倍
auto scaleTo = ScaleTo::create(4.0f, 2);
//原地旋转90度
auto rotateTo = RotateTo::create(2.0f, 90.0f);
//淡入 3s完成
auto fadeIn = FadeIn::create(3.0f);
//淡出 4s完成
auto fadeOut = FadeOut::create(4.0f);
//在5s里,透明度变化100
auto fadeTo = FadeTo::create(5.0f, 100);
auto actionTo = Sequence::create(scaleTo, rotateTo, fadeIn, fadeOut, fadeTo, NULL);
auto sprite_8 = Sprite::create("role1.png");
this->setPosition(Point(200,200));
this->addChild(sprite_8);
sprite_8->runAction(actionTo);2、TintTo和TintBy
//R G B 的取值范围都是0-255
TintTo::create(float duration, GLubyte red, GLubyte green, GLubyte blue);
TintBy::create(float duration, GLubyte red, GLubyte green, GLubyte blue);
//着色
auto tintTo = TintTo::create(2.0f, 100,255,30);3、Blink
//blinks为闪烁次数
Blink::create(float duration, int blinks);
//使其闪烁,2s内让闪烁10次
auto blink = Blink::create(2.0f, 10);4、Animation
以帧动画形式实现动画效果,以下代码用两种方法实现精灵帧动画效果
//创建一个空精灵
auto sprite_9 = Sprite::create();
sprite_9->setPosition(Point(160, 320));
this->addChild(sprite_9);
//创建一个空的动画
auto animation = Animation::create();
char szName[50];
for(int i = 1; i <= 18; ++i)
{
memset(szName, 0, sizeof(szName));
sprintf(szName, "role%d.png", i);
animation->addSpriteFrameWithFile(szName);
}
//18个精灵3s播完
animation->setDelayPerUnit(3.0/18);
//设置播放完保存原始图片
animation->setRestoreOriginalFrame(true);
//注意 这里是使用Animate,前面是Animation
auto animationAction = Animate::create(animation);
//播放一遍后就会消失
sprite_9->runAction(Sequence::create(animationAction, animationAction->reverse(), NULL));第二种使用plist文件
//文件创建动画
auto cache = AnimationCache::getInstance();
cache->addAnimationsWithFile("animations.plist");
auto animation2 = cache->getAnimation("dance_1");
auto action2 = Animate::create(animation2);
sprite_9->runAction(Sequence::create(action2, action2->reverse(), NULL));动画创建后需要一个动画播放器来播放这些动画,这个播放器就是Animate。
四、复合动作
通常在开发中我们需要将各种动作组合起来再让节点执行,复合动作的作用就是将各种动作组合在一起。而且,复合动作本身也是动作。因此可以作为一个普通动作嵌入到其他动作中。1、DelayTime
DelayTime::create(float d);d表示需要延时的时间。2、Repeat/RepeatForever
//循环次数
Repeat::create(FiniteTimeAction *action, unsigned int times);
//无限循环
RepeatForever::create(ActionInterval *action);
//创建一个空精灵
auto sprite_9 = Sprite::create();
sprite_9->setPosition(Point(160, 320));
this->addChild(sprite_9);
//创建一个空的动画
auto animation = Animation::create();
char szName[50];
for(int i = 1; i <= 18; ++i)
{
memset(szName, 0, sizeof(szName));
sprintf(szName, "role%d.png", i);
animation->addSpriteFrameWithFile(szName);
}
//18个精灵3s播完
animation->setDelayPerUnit(3.0/18);
//设置播放完保存原始图片
animation->setRestoreOriginalFrame(true);
//注意 这里是使用Animate,前面是Animation
auto animationAction = Animate::create(animation);
//无限循环
auto rep = RepeatForever::create(Sequence::create(animationAction, animationAction->reverse(), NULL));
sprite_9->runAction(rep);3、Spawn
使一批动作同时执行,他的两个初始化方法:
Spawn::create(FiniteTimeAction *action1, ...);
Spawn::create(const Vector<FiniteTimeAction*>& arrayOfActions);看参数大概就知道是什么样子的了,这里就不给出样例代码了4、Sequence
Sequence::create(FiniteTimeAction *action1, ...);
Sequence::create(const Vector<FiniteTimeAction*>& arrayOfActions);上面使用他无数次了,往前回顾五、变速动作
1、Speed
auto repeat = RepeatForever::create(animation);
auto speed = Speed::create(repeat, 0.5f);
sprite->run(speed);
第二个参数为变速比例,设置为0.5f则速度为原来一半。2、ActionEase
该类包含5类运动:
指数缓冲;
Sine缓冲;
弹性缓冲;
跳跃缓冲;
回震缓冲;
每类运动都包含3个不同时期的变换:In、Out和InOut。
由于这个实在太多了,我就只举一个例子
InSine例子:
auto sineIn = EaseSineIn::create(action);
sprite->runAction(sineIn);到此就总结的差不多了,如果后续还有,继续补充~~~~~~
// CCCardinalSpline // 作用:创建数组 点的数组 CCPointArray * array = CCPointArray::create(20); array->addControlPoint(ccp(0,0)); array->addControlPoint(ccp(210,0)); array->addControlPoint(ccp(210,240)); array->addControlPoint(ccp(0,160)); array->addControlPoint(ccp(0,0)); // CCCardinalSplineTo // 作用:创建一个样条曲线轨迹的动作 // 参数1:完成轨迹所需的时间 // 参数2:控制点的坐标数组 // 拟合度 其值= 0 路径最柔和 // CCActionInterval * CardinalSplineTo=CCCardinalSplineTo::create(3, array, 0); // sp->runAction(CardinalSplineTo); //CCCardinalSplineBy // 作用:创建一个样条曲线轨迹的动作 // 参数1:完成轨迹所需的时间 // 参数2:控制点的坐标数组 // 拟合度 其值= 0 路径最柔和 // CCActionInterval * CardinalSplineBy = CCCardinalSplineBy::create(3, array, 0); // sp->runAction(CardinalSplineBy); // CCCatmullRomTo CCCatmullRomBY // 作用:创建一个样条插值轨迹 // 参数1:完成轨迹的时间 // 参数2:控制点的数组坐标 // CCActionInterval * catmullRomTo = CCCatmullRomTo::create(3, array); // sp->runAction(catmullRomTo); // CCFollow // 作用:创建一个跟随动作 // 参数1:跟随的目标对象 // 跟随范围,离开范围就不再跟随 //创建一个参照物spT // CCSprite * spt = CCSprite::create("Icon.png"); // spt->setPosition(ccp(420,40)); // addChild(spt); // sp->runAction(CCMoveTo::create(3, ccp(940,sp->getPositionY()))); // // CCFollow * follow = CCFollow::create(sp,CCRectMake(0, 0, 960, 320)); // this-> runAction(follow); // CCEaseBounceIn // 目标动作 // CCActionInterval* move = CCMoveTo::create(3, ccp(300, sp->getPositionY())); //// 让目标动作缓慢开始 //// 参数:目标动作 // CCActionInterval * EaseBounceIn = CCEaseBounceIn::create(move); // sp->runAction(EaseBounceIn); //CCEaseBounceOut // 作用:让目标动作赋予反弹力,且以目标动作结束位子开始反弹 // 参数目标动作 // CCActionInterval * easeBounceOut = CCEaseBounceOut ::create(move); // sp->runAction(easeBounceOut); // CCEaseBounceInOut // 作用:让目标动作赋予反弹力,且以目标动作起始与结束位子开始反弹 // CCActionInterval * easeBounceInOut= CCEaseBounceInOut::create(move); // sp->runAction(easeBounceInOut); // CCEaseBackIn // 作用:让目标动作赋予回力 , 且以目标动作起点位置作为回力点 // 参数:目标动作 // CCActionInterval * easeBackIn = CCEaseBackIn::create(move); // sp->runAction(easeBackIn); // CCEaseBackOut // 作用:让目标动作赋予回力 , 且以目标动作终点位置作为回力点 // 参数:目标动作 // CCActionInterval *easeBackOut = CCEaseBackOut::create(move); // sp->runAction(easeBackOut); // CCEaseBackInOut // 作用:让目标动作赋予回力 , 且以目标动作起点和终点位置作为回力点 // 参数:目标动作 // CCActionInterval * easeBackInOut = CCEaseBackInOut::create(move); // sp->runAction(easeBackInOut); // CCEaseElasticIn // 作用:让目标动作赋予弹性 ,且以目标动作起点位子赋予弹性 // 参数:目标动作 // CCActionInterval * easeElasticIn= CCEaseElasticIn::create(move); // sp->runAction(easeElasticIn); // CCEaseElasticOut // 作用:让目标动作赋予弹性 ,且以目标动作终点位子赋予弹性 // 参数:目标动作 // CCActionInterval *easeElasticOut = CCEaseElasticOut::create(move); // sp->runAction(easeElasticOut); // CCEaseElasticInOut // 作用:让目标动作赋予弹性 ,且以目标动作起点和终点位子赋予弹性 // 参数:目标动作 // CCActionInterval *easeElasticInOut = CCEaseElasticOut::create(move); // sp->runAction(easeElasticInOut); // CCEaseExponentialIn // 让目标动作缓慢开始 // 参数:目标动作 // CCActionInterval * easeExponentialIn= CCEaseExponentialIn::create(move); // sp->runAction(easeExponentialIn); // CCEaseExponentialOut // 让目标动作缓慢中止 // 参数:目标动作 // CCActionInterval * easeExponentialIn= CCEaseExponentialOut::create(move); // sp->runAction(easeExponentialIn); // CCEaseExponentialInOut // 让目标动作缓慢开始和中止 // 参数:目标动作 // CCActionInterval * easeExponentialInOut= CCEaseExponentialInOut::create(move); // sp->runAction(easeExponentialInOut); // CCEaseRateAction // 作用 : 让目标动作设置速率 // 参数1:目标动作 // 参数2:速率 // CCActionInterval * move = CCMoveTo::create(5, ccp(300,sp->getPositionY())); // CCActionInterval * easeRateAction = CCEaseRateAction::create(move, 3); // sp->runAction(easeRateAction); // CCEaseSineIn // 作用:动作由慢到快 // 参数:目标动作 // CCActionInterval * easeSineIn = CCEaseSineIn::create(move); // sp->runAction(easeSineIn); // CCEaseSineOut // 作用:动作由快到慢 // 参数:目标动作 // CCActionInterval * easeSineOut = CCEaseSineOut::create(move); // sp->runAction(easeSineOut); // CCEaseSineInOut // 作用:动作由慢到快再快到慢 // 参数:目标动作 // CCActionInterval * easeSineInOut = CCEaseSineInOut::create(move); // sp->runAction(easeSineInOut);
本文详细介绍了cocos2dx中Action的使用,包括即时动作如Place、FlipX/Y、Show/Hide和CallFunc系列,持续动作如MoveTo/By、JumpTo/By,视觉特效动作如FadeIn/Out、TintTo/By,复合动作如DelayTime、Repeat/Forever、Spawn和Sequence,以及变速动作的运用。

238

被折叠的 条评论
为什么被折叠?



