首页 > 代码库 > Cocos2d-x3.0TestCpp目录笔记(二)

Cocos2d-x3.0TestCpp目录笔记(二)

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)),



如有错误,希望大家多多指正,谢谢。