Running a RepeatForever action immediately after another Action

I want to run a RepeatForever action on a sprite, but I want to run an initial Action on it first.

I’ve tried putting the initial Action inside a Sequence, followed by a CallFunc that assigns the RepeatForever action.

Sprite *sprite;

...

sprite = Sprite::create(...);

RepeatForever *repeatingAction = RepeatForever::create(...);

FiniteTimeAction *startAction = ...
CallFunc *switchToRepatingAction = CallFunc::create([&]()
{
    sprite->runAction(repeatingAction);
});
Sequence *startSequence = Sequence::create(startAction, switchToRepatingAction, nullptr);

sprite->runAction(startSequence);

I get a EXC_BAD_ACCESS error though when I get to sprite->runAction(repeatingAction) inside switchToRepatingAction’s lambda.

I’ll note that this is happening inside the init method of a Node subclass of which sprite is a member variable.

Is it also true that I can’t put a RepeatForever action directly inside a Sequence, even if I put it at the very end? I just tried doing this and my game locked up after the sprite ran startAction.

you need to retain the repeatingAction and release it inside of the lamda (after the runAction) to keep it alive until the lambda executes.

I added the retain and release. I still got an EXC_BAD_ACCESS error though, which was only fixed when I also changed the lambda from an & capture to an = capture.

try CallFunc::create([&, sprite]()
instead of CallFunc::create([&]()