Cocos-2d-x 学习笔记二(Action篇)

Cocos-2d-x学习笔记二

1.Action类

  • ActionInterval example:Flipx()
    ActionInstant是瞬时动作类,表示瞬间完成,中间没有动画效果,Flipx()就是它的一个例子,当物体走到障碍物时会有一个转向的过程,这可以通过Flipx的动作效果,以下是code:
//创建移动动作
ActionInterval *moveto = MoveTo::create(5, Vec2(0, 200));
// 创建X轴方向的翻转动作
ActionInstant *flipx = FlipX::create(true);
ActionInterval *moveback = MoveTo::create(5, Vec2(visibleSize.width, 200));
auto player=Sprite::create("player.png");
player->setPosition(Point(visibleSize.width/2,200));
auto action = Sequence::create(moveto, flipx, moveback, flipx->reverse(), NULL);   
player->runAction(action);
  • 延时动作
    即之前所提到的MoveBy,MoveTo会在指定时间内完成,中间会有动画效果,本次要将的是如何实现贝塞尔曲线的运动效果,它需要一个起点,终点,控制点,和控制线sample,代码实现如下:
ccBezierConfig beizer;
    beizer.controlPoint_1 = Point(200, 300);
    beizer.controlPoint_2 = Point(400, 400);
    beizer.endPosition = Point(50, 200);
    auto beizierAction = BezierTo::create(2.0f,beizer);
    auto dog = Sprite::create("player.png");
    dog->setPosition(Point(visibleSize.width / 2, 200));
    this->addChild(dog);
    dog->runAction(beizierAction);

额,后面会讲到一个JumpByJumpTo类所以就在这里先介绍一下,其create后面有4个参数,第一个是执行的时间,第二个是一个对象的坐标,代表跳完最后落在的坐标(以原来对象所处位置为原点的相对坐标系)
- ActionEase 缓冲效果
该类有5类运动:指数缓冲,Sine缓冲,弹性缓冲,跳跃缓冲,回震缓冲,每类运动都包含了3个不同时期的变换:In,Out,InOut,(其中in代表开始时加速,out代表结尾时加速),以下是样例:

ActionInterval *moveto1 = MoveTo::create(5, Vec2(50, 100));
ActionInterval *moveto2 = MoveTo::create(5, Vec2(50, 300));
ActionInterval *moveto3 = MoveTo::create(5, Vec2(50, 500));
auto scene = Director::getInstance()->getRunningScene();
auto layer = scene->getChildByTag(1);
auto dog1 = layer->getChildByTag(1);
auto dog2 = layer->getChildByTag(2);
auto dog3 = layer->getChildByTag(3);
auto action1 = EaseExponentialIn::create(moveto1);
auto action2 = EaseExponentialOut::create(moveto2);
auto action3 = EaseExponentialInOut::create(moveto3);
dog1->runAction(action1);
dog2->runAction(action2);
dog3->runAction(action3);    //我在vs上跑的时候为什么先执行第二个函数
  • 组合动作
    sequence和spawn在上一次中已经讲过了,所以这次讲一下RepeatRepeatForever类。repeat类可以指定重复次数。代码示例如下:

    auto jumpto1 = JumpBy::create(3, Vec2(0, 0), 100, 1);
    auto jumpto2 = JumpBy::create(3, Vec2(0, 0), 100, 1);
    auto player1 = Sprite::create("player.png");
    auto player2 = Sprite::create("player.png");
    player1->setPosition(Point(visibleSize / 3));
    player1->setPosition(Point(visibleSize * 2 / 3));
    this->addChild(player1);
    this->addChild(player2);
    auto repeatJump = Repeat::create(jumpto1, 5);
    auto repeatForeverJump = RepeatForever::create(jumpto2);
    player1->runAction(repeatJump);
    player2->runAction(repeatForeverJump);

    其实还有一个callfunc类没有写,但是目前还是没有完全搞懂这一个系列的东西的作用到底是啥,所以就先不写了。。。

你可能感兴趣的:(cocos)