Cocos2dx. Creating a sequence that contains RepeatForever

300 views Asked by At

I need to create a sequence that runs one action, then repeats tho other actions forever. But creating a sequence with an action and a RepeatForever is impossible. You can see my code below:

auto firstRight = MoveTo::create(1.0f, Vec2(mTopRightPos, spriteYPos));
auto moveRight = MoveTo::create(2.0f, Vec2(mTopRightPos, spriteYPos));
auto moveLeft = MoveTo::create(2.0f, Vec2(mTopLeftPos, spriteYPos));

auto seq = Sequence::create(moveLeft, moveRight, nullptr);
auto repeat = RepeatForever::create(seq);

auto mainSeq = Sequence::create(firstRight, repeat);
mSprite1->runAction(mainSeq);
1

There are 1 answers

0
Lazy Cushion On

Create the repeat action in callback.

auto firstActionCallback = CallFunc::create([](){
    auto moveRight = MoveTo::create(2.0f, Vec2(mTopRightPos, spriteYPos));
    auto moveLeft = MoveTo::create(2.0f, Vec2(mTopLeftPos, spriteYPos));
    auto seq = Sequence::create(moveLeft, moveRight, nullptr);
    auto repeat = RepeatForever::create(seq);
    mSprite1->runAction(repeat);
});

auto firstRight = MoveTo::create(1.0f, Vec2(mTopRightPos, spriteYPos));
auto mainSeq = Sequence::create(firstRight, firstActionCallback );
mSprite1->runAction(mainSeq);