3.Actions-Basic:此demo中体现ccp由Point取代
①ActionManual:直接设置精灵的属性demo。
const Color3B Color3B::RED (255, 0, 0);
const Color3B Color3B::GREEN ( 0, 255, 0);
const Color3B Color3B::BLUE ( 0, 0, 255);
//第四个參数为透明度,前三个同上
const Color4B Color4B::RED (255, 0, 0, 255);
const Color4B Color4B::GREEN ( 0, 255, 0, 255);
const Color4B Color4B::BLUE ( 0, 0, 255, 255);
//參数同上,就是用比例方式传參
const Color4F Color4F::WHITE ( 1, 1, 1, 1);
const Color4F Color4F::YELLOW ( 1, 1, 0, 1);
const Color4F Color4F::GREEN ( 0, 1, 0, 1);
const Color4F Color4F::BLUE ( 0, 0, 1, 1);
const Color4F Color4F::RED ( 1, 0, 0, 1);
const Color4F Color4F::MAGENTA( 1, 0, 1, 1);
const Color4F Color4F::BLACK ( 0, 0, 0, 1);
const Color4F Color4F::ORANGE ( 1, 0.5f, 0, 1);
const Color4F Color4F::GRAY (0.65f, 0.65f, 0.65f, 1);
//以下的求解释
const BlendFunc BlendFunc::DISABLE = {GL_ONE, GL_ZERO};
const BlendFunc BlendFunc::ALPHA_PREMULTIPLIED = {GL_ONE, GL_ONE_MINUS_SRC_ALPHA};
const BlendFunc BlendFunc::ALPHA_NON_PREMULTIPLIED = {GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA};
const BlendFunc BlendFunc::ADDITIVE = {GL_SRC_ALPHA, GL_ONE};
②ActionMove:MoveTo/MoveBy。使用方法:此动作是移动。
auto actionTo = MoveTo::create(2, Point(s.width-40, s.height-40));
auto actionBy = MoveBy::create(2, Point(80,80));
auto actionByBack = actionBy->reverse();
③ActionRotate:RotateBy/RotateTo。使用方法:此动作是旋转。
auto actionTo = RotateTo::create( 2, 45);
auto actionBy = RotateBy::create(2 , 360);
auto actionByBack = actionBy->reverse();
④ActionRotateBy3D:RotateBy。在3D世界旋转。X,Y坐标的基点是左下角,Z坐标的基点就是中心?
从源代码看出,仅仅有RotateBy有,使用方法:
auto actionBy1 = RotateBy::create(4, Vertex3F(360, 0, 0));
auto actionBy2 = RotateBy::create(4, Vertex3F(0, 360, 0));
auto actionBy3 = RotateBy::create(4 ,Vertex3F(0, 0, 360));
⑤ActionScale:ScaleTo/ScaleBy。此动作是设置缩放,參数大于零放大,小于一是缩小,负数为翻转。
使用方法:
auto actionTo = ScaleTo::create(2.0f, 0.5f);
auto actionBy = ScaleBy::create(2.0f, 1.0f, 10.0f);
auto actionBy2 = ScaleBy::create(2.0f, 5.0f, 1.0f);
⑥ActionSkew:SkewTo/SkewBy。此动作是设置倾斜。使用方法:(此动作不懂)
auto actionTo = SkewTo::create(2, 37.2f, -37.2f);
auto actionToBack = SkewTo::create(2, 0, 0);
auto actionBy = SkewBy::create(2, 0.0f, -90.0f);
auto actionBy2 = SkewBy::create(2, 45.0f, 45.0f);
⑦ActionRotationalSkew:相同是RotateBy/RotateTo。仅仅只是是两个參数,各自是X和Y。用单独的旋转角度。
使用方法:
auto actionByBack = actionBy->reverse();
auto actionTo = RotateTo::create(2, 180, 180);
auto actionToBack = RotateTo::create(2, 0, 0);
auto actionBy = RotateBy::create(2, 0.0f, 360);
auto actionByBack = actionBy->reverse();
auto actionBy2 = RotateBy::create(2, 360, 0);
auto actionBy2Back = actionBy2->reverse();
⑧ActionRotationalSkewVSStandardSkew:标准Skew和Rotate比較,据发现Skew会牵扯到缩放系数。
使用方法同上。
⑨ActionSkewRotateScale:三个动作同一时候运行。SizeMake改为Size。
使用方法:
box->runAction(Sequence::create(actionTo, actionToBack, NULL));
box->runAction(Sequence::create(rotateTo, rotateToBack, NULL));
box->runAction(Sequence::create(actionScaleTo, actionScaleToBack, NULL));
⑩ActionJump:JumpTo/JumpBy。不用解释。
基本动作完毕,综上总结Skew和Rotate都为差别为,Skew改变了节点的缩放系数,详细改变求解说。
下面为特殊动作。她们是在一个菜单里的,为Actions-Basic.
①CardinalSplineBy / CardinalSplineTo:事实上就调用了一个CardinalSplineBy,其动作运行为
四个控制点的一个矩形。draw方法的内容MARK一下,求大神传授。。。
使用方法:
CCCardinalSplineBy::create(float duration, cocos2d::CCPointArray *points, float tension);
中duration是时间间隔,points是控制点列表,tension是松紧程度。tension==1时,样条线是分段直线。
tension<1向外松弛弯曲,tension>1向内缩紧弯曲。By动作是以当前坐标为新坐标原点。
auto array = PointArray::create(20);
array->addControlPoint(Point(0, 0));
array->addControlPoint(Point(s.width/2-30, 0));
array->addControlPoint(Point(s.width/2-30, s.height-80));
array->addControlPoint(Point(0, s.height-80));
array->addControlPoint(Point(0, 0));
auto action = CardinalSplineBy::create(3, array, 0);
auto reverse = action->reverse();
auto seq = Sequence::create(action, reverse, NULL);
②CatmullRomBy / CatmullRomTo;同上一样是曲线依照控制点运动,详细的我还没有搜索到,请知道的给我说下。谢谢。
使用方法:
auto array = PointArray::create(20);
array->addControlPoint(Point(0, 0));
array->addControlPoint(Point(80, 80));
array->addControlPoint(Point(s.width - 80, 80));
array->addControlPoint(Point(s.width - 80, s.height - 80));
array->addControlPoint(Point(80, s.height - 80));
array->addControlPoint(Point(80, 80));
array->addControlPoint(Point(s.width / 2, s.height / 2));
auto action = CatmullRomBy::create(3, array);
auto reverse = action->reverse();
auto seq = Sequence::create(action, reverse, NULL);
③BezierBy / BezierTo:贝尔曲线,使用方法:
ccBezierConfig bezier;
bezier.controlPoint_1 = Point(0, s.height/2);
bezier.controlPoint_2 = Point(300, -s.height/2);
bezier.endPosition = Point(300,100);
auto bezierForward = BezierBy::create(3, bezier);
auto bezierBack = bezierForward->reverse();
auto rep = RepeatForever::create(Sequence::create( bezierForward, bezierBack, NULL));
④ActionBlink:Blink闪烁动作,使用方法:
// 持续时间
闪烁次数
static Blink* create(float duration, int blinks);
auto action1 = Blink::create(2, 10);
auto action2 = Blink::create(2, 5);
⑤ActionFade:FadeIn / FadeOut此动作为改变节点的透明度属性,当中FadeIn为全然显示,FadeOut为全然隐藏。
使用方法:
//參数为delay延时单位为秒
static FadeIn* create(float d);
auto action1 = FadeIn::create(1.0f);
auto action1Back = action1->reverse();
⑥ActionTint:TintTo / TintBy此动作为改变节点颜色属性。使用方法:
static TintTo* create(float duration, GLubyte red, GLubyte green, GLubyte blue);
參数列表为,duration,和R,G,B。
auto action1 = TintTo::create(2, 255, 0, 255);
auto action2 = TintBy::create(2, -127, -255, -127);
auto action2Back = action2->reverse();
⑦Animation:动画demo。动画也有reverse()方法。
使用方法一:通过普通文件创建动画。
auto animation = Animation::create();
for( int i=1;i<15;i++)
{
char szName[100] = {0};
sprintf(szName, "Images/grossini_dance_%02d.png", i);
animation->addSpriteFrameWithFile(szName);
}
// should last 2.8 seconds. And there are 14 frames.
animation->setDelayPerUnit(2.8f / 14.0f);
animation->setRestoreOriginalFrame(true);
auto action = Animate::create(animation);
_grossini->runAction(Sequence::create(action, action->reverse(), NULL));
使用方法二:通过plist创建动画
auto cache = AnimationCache::getInstance();
cache->addAnimationsWithFile("animations/animations-2.plist");
auto animation2 = cache->getAnimation("dance_1");
auto action2 = Animate::create(animation2);
_tamara->runAction(Sequence::create(action2, action2->reverse(), NULL));
注意:animations-2.plist文件和其它plist文件不一样。
⑧⑨Sequence: Move + Rotate:顺序运行n个动作。使用方法:
static Sequence* create(FiniteTimeAction *action1, ...) CC_REQUIRES_NULL_TERMINATION;
//參数以NULL结尾。
⑩问题 CallFunc中的create方法的调用 參数不同,调用的是不是相应的CalkFunc家族的类。
auto action = Sequence::create(
Place::create(Point(200,200)),
Show::create(),
MoveBy::create(1, Point(100,0)),
CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback1,this)),
CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback2,this,_grossini)),
CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback3,this,_grossini,0xbebabeba)),
NULL);
(11)"Sequence: Move + Rotate + Scale + RemoveSelf":移除自己动作,使用方法:
static RemoveSelf * create(bool isNeedCleanUp = true);
auto action = Sequence::create(
MoveBy::create( 2, Point(240,0)),
RotateBy::create( 2, 540),
ScaleTo::create(1,0.1f),
RemoveSelf::create(),
NULL);
(12)Spawn: Jump + Rotate:函数为:ActionSpawn:几个动作同一时候运行。
使用方法:
auto action = Spawn::create(
JumpBy::create(2, Point(300,0), 50, 4),
RotateBy::create( 2, 720),
NULL);
(13)Reverse an action:函数为:ActionReverse,动作的恢复。
使用方法:
auto jump = JumpBy::create(2, Point(300,0), 50, 4);
auto action = Sequence::create( jump, jump->reverse(), NULL);
(14)DelayTime: m + delay + m:函数为:ActionDelayTime,延时动作。
使用方法:
auto move = MoveBy::create(1, Point(150,0));
//延时动作的參数单位为秒
auto action = Sequence::create( move, DelayTime::create(2), move, NULL);
(15)Repeat / RepeatForever actions:函数为:ActionRepeat:
使用方法:
//反复三次指定动作,当中Place动作是放置节点到指定位置,就是把setPosition弄成了动作。
//当中最后的參数“3”就是反复次数
auto action1 = Repeat::create(Sequence::create( Place::create(Point(60,60)), a1, NULL) , 3);
//无休止的反复指定动作。
auto action2 = RepeatForever::create(Sequence::create(a1->clone(), a1->reverse(), NULL));
(16)CallFuncN + RepeatForever:函数为:ActionRepeatForever,通过回调函数使节点无休止运行指定动作。
使用方法:
auto action = Sequence::create(DelayTime::create(1),
CallFunc::create( std::bind( &ActionRepeatForever::repeatForever, this, _grossini) ),
NULL);
void ActionRepeatForever::repeatForever(Node* sender)
{
auto repeat = RepeatForever::create( RotateBy::create(1.0f, 360) );
sender->runAction(repeat);
}
(17)Repeat/RepeatForever + RotateTo:函数为: ActionRotateToRepeat ,疑问,为什么当精灵_kathia运行完动作后会旋转90°,个人感觉是bug。
还有就是“clone()方法”的使用方法。
使用方法:
auto act1 = RotateTo::create(1, 90);
auto act2 = RotateTo::create(1, 0);
auto seq = Sequence::create(act1, act2, NULL);
auto rep1 = RepeatForever::create(seq);
auto rep2 = Repeat::create( seq->clone(), 10);
(18)RepeatForever / Repeat + Rotate:函数为:ActionRotateJerk。同上 仅仅是角度不同。
(19)Callbacks: CallFunc with std::function():函数为: ActionCallFunction 。解说各种方式创建CallFunc系列动作(多态创建)。
源代码:疑点,參数this的作用,调用的是哪个create函数。
auto action1 = Sequence::create(
MoveBy::create(2, Point(200,0)),
CallFunc::create( std::bind(&ActionCallFunction::callback1, this) ),
CallFunc::create(
// lambda 表达式
[&](){
auto s = Director::getInstance()->getWinSize();
auto label = Label::createWithTTF("called:lambda callback", "fonts/Marker Felt.ttf", 16.0f);
label->setPosition(Point( s.width/4*1,s.height/2-40));
this->addChild(label);
} ),
NULL);
auto action2 = Sequence::create(
ScaleBy::create(2 , 2),
FadeOut::create(2),
CallFunc::create( std::bind(&ActionCallFunction::callback2, this, _tamara) ),
NULL);
auto action3 = Sequence::create(
RotateBy::create(3 , 360),
FadeOut::create(2),
CallFunc::create( std::bind(&ActionCallFunction::callback3, this, _kathia, 42) ),
NULL);
void ActionCallFunction::callback1();
void ActionCallFunction::callback2(Node* sender);
void ActionCallFunction::callback3(Node* sender, long data);
(20)Grossini should jump after moving:函数为: ActionCallFuncN 。须要重看。
源代码:
auto action = Sequence::create(
MoveBy::create(2.0f, Point(150,0)),
CallFuncN::create( CC_CALLBACK_1(ActionCallFuncN::callback, this)),
//假设用CallFuncN创建CallFuncN系列的回调函数,则不须要传递Node*參数。(?)
NULL);
_grossini->runAction(action);
void ActionCallFuncN::callback(Node* sender )
{
auto a = JumpBy::create(5, Point(0,0), 100, 5);
sender->runAction(a);
}
(21)simulates CallFuncND with std::bind():函数为:ActionCallFuncND:移动后自己主动消失。
源代码:
auto action = Sequence::create(
MoveBy::create(2.0f, Point(200,0)),
CallFuncN::create( CC_CALLBACK_1(ActionCallFuncND::doRemoveFromParentAndCleanup, this, true)),
//用CallFuncN的方法创建动作,回调函数还能带有和传其它參数?
NULL);
_grossini->runAction(action);
void ActionCallFuncND::doRemoveFromParentAndCleanup(Node* sender, bool cleanup)
{
_grossini->removeFromParentAndCleanup(cleanup);
}
CallFunc系列总结:能够用CallFunc::create()方法创建这个系列的回调函数,仅仅要參数不同就可以。第二个參数固定为this,意识是这个函数的
调用者,使用方法例如以下所看到的:
CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback1,this)),
CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback2,this,_grossini)),
CallFunc::create( CC_CALLBACK_0(ActionSequence2::callback3,this,_grossini,0xbebabeba)),
如有错误,希望大家多多指正,谢谢。