第八章动作和动画-组合动作

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/RandomWW/article/details/54614510

Cocos2d-x学习笔记


组合动作类型:顺序、并列、有限次重复、无限次重复、反动作和动画。

实例(此处不再粘贴所有代码。)

void Setting::OnSequence(cocos2d::Ref * pSender)
{
    Size size = Director::getInstance()->getVisibleSize();
    Vec2 p = Vec2(size.width / 2, 200);

    FiniteTimeAction* ac0 = (FiniteTimeAction*)sprite->runAction(Place::create(p));
    FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(MoveTo::create(4, Vec2(size.width - 130, size.height - 200)));
    FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(JumpBy::create(2, Vec2(8, 8), 6, 3));
    FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(Blink::create(2, 3));
    FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction(TintBy::create(0.5, 0, 255, 255));

    sprite->runAction(Sequence::create(ac0, ac1, ac2, ac3, ac4, ac0, NULL));
}

void Setting::OnSpawn(cocos2d::Ref * pSender)
{
    Size size = Director::getInstance()->getVisibleSize();
    Vec2 p = Vec2(size.width / 2, 200);

    FiniteTimeAction* ac0 = (FiniteTimeAction *)sprite->runAction(MoveTo::create(2, Vec2(size.width - 100, size.height - 100)));
    FiniteTimeAction* ac1 = (FiniteTimeAction *)sprite->runAction(RotateTo::create(2, 40));

    sprite->runAction(Spawn::create(ac0, ac1, NULL));
}

void Setting::OnRepeat(cocos2d::Ref * pSender)
{
    Size size = Director::getInstance()->getVisibleSize();
    Vec2 p = Vec2(size.width / 2, 200);

    sprite->setRotation(0);
    sprite->setPosition(p);

    FiniteTimeAction* ac0 = (FiniteTimeAction *)sprite->runAction(MoveTo::create(2, Vec2(size.width - 100, size.height - 100)));
    FiniteTimeAction* ac1 = (FiniteTimeAction *)sprite->runAction(JumpBy::create(2, Vec2(10, 10), 20, 5));
    FiniteTimeAction* ac2 = (FiniteTimeAction *)sprite->runAction(JumpBy::create(2, Vec2(-10, -10), 20, 3));

    ActionInterval* seq = Sequence::create(ac0, ac1, ac2, NULL);

    sprite->runAction(Repeat::create(seq, 3));
}

void Setting::OnRepeatForever(cocos2d::Ref * pSender)
{
    Size size = Director::getInstance()->getVisibleSize();
    Vec2 p = Vec2(size.width / 2, 500);

    sprite->setRotation(0);
    sprite->setPosition(p);

    ccBezierConfig bezier;
    bezier.controlPoint_1 = Vec2(0, size.height / 2);
    bezier.controlPoint_2 = Vec2(10, -size.height / 2);
    bezier.endPosition = Vec2(10, 20);

    FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(BezierBy::create(2, bezier));
    FiniteTimeAction* ac2 = (FiniteTimeAction*)sprite->runAction(TintBy::create(0.5, 0, 255, 255));
    FiniteTimeAction* ac1Reverse = ((ActionInterval*)ac1)->reverse();
    FiniteTimeAction* ac2Repeat = (FiniteTimeAction*)sprite->runAction(Repeat::create((ActionInterval*)ac2, 4));

    FiniteTimeAction* ac3 = (FiniteTimeAction*)sprite->runAction(Spawn::create(ac1, ac2Repeat, NULL));

    FiniteTimeAction* ac4 = (FiniteTimeAction*)sprite->runAction(Spawn::create(ac1Reverse, ac2Repeat, NULL));

    ActionInterval* seq = Sequence::create(ac3, ac4, NULL);

    sprite->runAction(RepeatForever::create(seq));
}

void Setting::OnReverse(cocos2d::Ref * pSender)
{
    Size size = Director::getInstance()->getVisibleSize();
    Vec2 p = Vec2(size.width / 2, 300);

    sprite->setRotation(0);
    sprite->setPosition(p);

    FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(MoveBy::create(2, Vec2(40, 60)));
    Action* ac2 = ac1->reverse();

    ActionInterval* seq = Sequence::create(ac1, ac2, NULL);

    sprite->runAction(Repeat::create(seq, 2));
}

代码并不难懂,只是在实验的时候有一点疑惑,按照我个人的理解,组合动作是现将动作写出,后组合再执行。但是在测试的过程中,类似于FiniteTimeAction* ac1 = (FiniteTimeAction*)sprite->runAction(MoveTo::create(4, Vec2(size.width - 130, size.height - 200)));这样的语句同样会执行动作,即我将sprite->runAction(Sequence::create(ac0, ac1, ac2, ac3, ac4, ac0, NULL));此句注释掉,上述创建动作的四个语句会执行动作,而且看起来像是并行动作。对于这一点,和我个人的理解有些不同。望有大神解释下。

没有更多推荐了,返回首页