cocos2dx-3.0(28) 动作类 Action

来源:互联网 发布:七天网络怎么绑定不了 编辑:程序博客网 时间:2024/05/22 13:07

~~~~我的生活,我的点点滴滴!!


我们简单看看Action的继承树状图:


动作类(Action)是所有动作的基类,它创建的一个对象代表一个动作。动作作用于Node,因此每个动作都需要由Node对象执行。动作类(Action)作为基类,实际上是一个接口,动作类的大多数实现类都派生于有限时间动作类(FiniteTimeAction)。

但是在实际开发中我们通常用到两类动作-即时动作和持续动作,它们均继承于有限时间动作类。


一、即时动作

即时动作是能够立刻完成的动作,这类动作是在下一帧立刻完成的动作,如设定位置、设定缩放等。把它们包装成动作后,可以与其他动作类组合为复杂动作。


下面介绍一些常用即时动作


1、Place

该动作用于将节点放置到某个指定位置,其作用与修改节点的position属性相同。例如,将节点放到屏幕坐标(50, 200)处的代码如下:

//在Point(50,200)处放一个nodeauto 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),使用2sauto moveToActionGo = MoveTo::create(2.0f, Point(300,500));//移到Point(50,50),使用2sauto 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),使用1sauto moveToAction_1 = MoveTo::create(1.0f, Point(100,100));//移到Point(200,200),使用1sauto moveToAction_2 = MoveTo::create(1.0f, Point(200,200));//移到Point(300,300),使用1sauto 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使用的不同写法

//写法1auto action1 = CallFunc::create( CC_CALLBACK_0(MyClass::callback_0,this));auto action2 = CallFunc::create( CC_CALLBACK_0(MyClass::callback_1,this, additional_parameters));//写法2auto 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); 
//写法1auto action = Sequence::create(MoveBy::create(2.0f, Point(150,0)),CallFuncN::create( CC_CALLBACK_1(ActionCallFuncN::callback, this)),NULL);//写法2auto action = Sequence::create(   MoveBy::create(2.0f, Point(150,0)),   CallFuncN::create(std::bind(&ActionCallFuncN::callback,this,std::placeholders::_1)),   NULL);   //写法3auto 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  的回调动作。


二、持续动作

属性变化动作:属性变化动作通过属性值的逐渐变化来实现动画效果。需要注意的是XXTo和XXBy的区别在于XXTo是表示最终值,而XXBy则表示向量-改变值。

1、MoveTo和MoveBy


用于使节点做直线运动,设置了动作时间和终点位置,在规定时间内会移动到终点,它们的初始化方法如下:
//移动到Position的地方MoveTo::create(float duration, const Point& position);//移动的改变值为PositionMoveBy::create(float duration, const Point& position);

很多时候他两的效果相似。


2、JumpTo和JumpBy


使节点以一定的轨迹跳跃到指定位置,JumpTo 是跳到Position位置,JumpBy 是跳起一段距离改变值为Position。

先看看他们声明:
    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结构体,设置好终点endPosition以及两个两个控制点controlPoint_1和controlPoint_2后,再把结构体传入BezierTo或BezierBy的初始化方法中:
//先声明一个贝尔结构体ccBezierConfig bezier;//设置目标位置bezier.endPosition = Point(50, 400);//设置控制点1bezier.controlPoint_1 = Point(0,0);//设置控制点2bezier.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里,透明度变化100auto 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-255TintTo::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,前面是Animationauto 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

四、复合动作

通常在开发中我们需要将各种动作组合起来再让节点执行,复合动作的作用就是将各种动作组合在一起。而且,复合动作本身也是动作。因此可以作为一个普通动作嵌入到其他动作中。
特别注意:Sequence动作不能嵌入其他复合动作内使用,DelayTime不属于复合动作,但是只能在复合动作内使用。

1、DelayTime

延时动作其实什么都不做,提供一段空白期,它只有一个初始化方法:
    DelayTime::create(float d);
d表示需要延时的时间。

2、Repeat/RepeatForever

反复执行某个动作,通常我们用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,前面是Animationauto 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


用于线性的改变某个动作的速度,为了改变一个动作的速度,首先需要将目标动作包装到Speed动作中:
auto repeat = RepeatForever::create(animation);auto speed = Speed::create(repeat, 0.5f);sprite->run(speed);
第二个参数为变速比例,设置为0.5f则速度为原来一半。


2、ActionEase


Speed虽然能改变动作的速度,但是只能按比例改变速度,ActionEase可以实现动作的速度由快到慢、速度随时间改变的匀速运动。
该类包含5类运动:
指数缓冲;
Sine缓冲;
弹性缓冲;
跳跃缓冲;
回震缓冲;
每类运动都包含3个不同时期的变换:In、Out和InOut。
由于这个实在太多了,我就只举一个例子
InSine例子:
auto sineIn = EaseSineIn::create(action);sprite->runAction(sineIn);


          到此就总结的差不多了,如果后续还有,继续补充~~~~~~

随便上张效果图






-----------------------------------------------------------------------------------------------------------------------------------------------------------以下是3.x以前的,不确实还有没有。没研究,太多了
//    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);


0 0
原创粉丝点击