Collapsed PLUG Collapsed PLUG - 1 year ago 39
iOS Question

How can I 'reuse' cocos2d::action?

I'm trying to make an animation sequence with cocos2d-x. What I want to make is sprites moving downwards by certain distance one after another. However, I'm coding very awfully.

What I tried to do first is this (I think you'll get what I mean clearer with this):

auto FallAction = MoveBy::create(0.2f, Vec2(0, -director->getWinSize().height));
auto FallActionEase = EaseIn::create(FallAction, 2.0f);
auto FallStretch = ScaleBy::create(0.1f,1.0f, 1.2f);
auto Fall = Spawn::create(FallActionEase, FallStretch, NULL);
auto LandTremble = EaseElasticOut::create((ScaleTo::create(0.5f,_finalScale)));

this->getK()->runAction(Sequence::create(Delay::create(0.5f), Fall, LandTremble));
this->getA()->runAction(Sequence::create(Delay::create(1.0f), Fall, LandTremble));
this->getW()->runAction(Sequence::create(Delay::create(1.5f), Fall, LandTremble));

But this doesn't work, as discussed here.

Then I found that I can copy actions, but then I also found that Clonable::copy() is now deprecated (seems that it doesn't even exist in v3.6!)

I ended up nesting lambdas. I won't write here because it's way too UGLY. See it here, the code in question is from line 246 to 254.

But I want to 'reuse'
in a good manner! What I want the code to be are:

  • make the very same animation mentioned above

  • still
    ing only one
    to determine the moves of sprites

  • and easy-to-maintenance.

Any idea how to do this?

Answer Source

How about this:

Action* createSequence(float delay)
    auto fall = Spawn::create(
        EaseIn::create(MoveBy::create(0.2f, Vec2(0, -director->getWinSize().height)), 2.0f),
        ScaleBy::create(0.1f, 1.0f, 1.2f),
    auto landTremble = EaseElasticOut::create((ScaleTo::create(0.5f, _finalScale)));
    return Sequence::create(Delay::create(delay), fall, landTremble, NULL);

then you can use this function: