A simple way to loop animation for a duration

For my pathing of my character, I sequence animations and use callfunc to cancel the previous animation action, before starting a new one.

This is just such a crummy way to switch animations.

I was hoping I could just run an animation on loop for a duration, but I couldn’t find any such function or documentation on such a thing.

There must be something better for advanced animations than cc.Animation.create.

Just repeat forever.

cc.stopAllActions(); 
// or set tag and stop by tag if you run multiple parallel actions
cc.RepeatForever.create(cc.Animate.create(animation));

That means for each animation I would have to write:
cc.RepeatForever.create(cc.Animate.create(animation));
cc.stopAllActionsByTag();

Considering I switch animations based on the direction my character faces as he’s pathing, this means I have to do two functions calls per direction switch, instead of 1, because there is apparently no better way?

Preferably I could just write:
runAnimationForDuration(frames, speed, duration)
… and there is no reason I couldn’t write this function myself and just return an action that contains both animating and stopping the animation after a set time.

Hmmm I’m going to ask a new question with better framing. Because I think the main use case for this is pathing a character animation with multiple orientations.

For example;

auto animation = Animation::createWithSpriteFrames(animFrames, duration);

you should add duration argument when you create animation object.

@jaman4dbz Yeah we actually just stop and then run a forever animate action on every direction change. We don’t yet bother with worrying about keeping the current frame index for smoother animations (Edit: party because we’re in c++ land and can worry less about excess allocations, at least compared to JS). We do have a develop branch where we’ve taken the internal Animate code and used it to change the sprite frame directly instead of using actions. This allows for changing directions and then switching the animation frames used while keeping the current frame index. It’s relatively simple and it’s probably the way you should look toward since it eliminates tons of excess allocations (& GC hiccups) as well as allows for smoother and more customized animations.

Actions are awesome and a great feature of this and other engines, but they’re not the end-all-be-all for animating and writing your own animator is a normal thing to do when the problem warrants it as a solution. I think the custom solution would be best for you.

Maybe you could wrap it up into an action (or we’ll wrap ours up once debugged and finished) and offer it as an outside 3rd-party action or send it up as a PR to the github for integration in a future engine version. I’m not positive this would be easily to wrap up as it’s someone game-specific.

1 Like

Thanks for the anecdote.
Ya, honestly the proper way would be to use update. I should just do it that way.

some kind of wrapper well be necessary, but it isn’t a straight forward thing, nor something that everyone would use, so it makes sense something custom would likely need to be made for this scenario.