How to pause execution until an animation/animator finishes?

When a player makes an input, I would like to have a sprite move along a path, and then after it is finished have the game complete some other actions. What is the best way to do this?

One approach I tried is roughly the following (where toothbrushSmall is a sprite and lineSeg is the line segment I want to move along):

local anim =,lineSeg)
toothbrushSmall:setAnimator(anim, playdate.easingFunctions.outQuad)
while not anim:ended() do

but this causes the FPS to go out of control and I also seem to be crashing the simulator quite often. An alternative is to only do the stuff inside the loop every 33 or 34 milliseconds while the animation is running, but this causes timing errors I don't understand. In particular, there are long delays around playdate.update() getting called again, and unexpected delays when calling playdate.wait().

I'm attaching a little demo where I was trying to isolate what goes wrong. Pushing a directional button will cause a sprite to move out, delay momentarily, and then move back, with one of the two strategies. There are also two separate delay functions, one using playdate.wait() and the other using a loop. Pressing B will toggle whether the delay function is ever used.

I mentioned that the first strategy sometimes crashes the simulator. Interestingly, :arrow_down:, which uses playdate.wait() often crashes the simulator for me, while :arrow_up: which uses a loop does not, even when the delay function is not used! (30.3 KB)

platform: macOS (M1)
SDK: 1.9.3

Try putting your code in the sprite's update function. That code is run every time the Sprite is updated which feels right for your task.

You might also find Nic's sequence library useful. It may not do what you want, exactly, but will have some good ideas in its code.