Sprite:update vs sprite.update - how to stop sprite animation on button press

Forgive me coders, for I have sinned. It's be over 25 years since I've coded last. I'm on macOS 12.3.1 and have a relatively simple question that I seem to be overlooking the answer to: sprite:update vs sprite.update

The docs say sprite:update allows for code to run on every update. The code I want is pretty simple - if on A button down, stop all animations (with a speed ramp down than I've yet to think about). On B down, resume all animations.

Q1: Does sprite:update get called by playdate.graphics.sprite.update? So all I have to do is declare a sprite.new and then a sprite:update function on the new sprite?

Q2: I'm going to use a global state variable "IsRunning" to determine whether to stop the animation on the playdate.update level but I think the best thing to do is simply not update specific sprites as opposed to stopping all sprite animations. (In the future, I might be selecting which sprites I want to stop their animations on.) Does this seem like the right approach?

Thanks for helping an older noob.

Q1: Yes, pretty much. When you call sprite.update(), the system figures out which sprites 1) are on the display list (due to having been add()ed), and 2) have updates enabled (via sprite:setUpdatesEnabled(true) which is the default). Then it calls each such sprite's own update() method.

Q2: I would just turn updateEnabled off for the sprites that you want not to animate.

Also, note that if you want to temporarily stop playdate.update() from being called entirely (for example, if your game does little or nothing between button presses), you can do that using playdate.wait() or playdate.stop().

Thanks. Got me half the way there. I can't seem to restart the sprite updates

carRolling is a global based on a button press in playdate.update. If A is pressed, the global is set to false. If B is pressed, it's set to true. Either/neither way, playdate.graphics.sprite.update is called.

function playdate.update()

if playdate.buttonJustPressed( playdate.kButtonA ) then
	carRolling = false
elseif playdate.buttonJustPressed( playdate.kButtonB ) then
	carRolling = true
end
gfx.sprite.update()
playdate.timer.updateTimers()

end

This snippet is in my sprite updater:

if carRolling == true then
	spriteL.setUpdatesEnabled(true)
	spriteL.setImage(spriteImagesL[spriteL.i])

elseif carRolling == false then
	spriteL.setUpdatesEnabled(false)
end

What am I missing? (Also the documentation for playdate.graphics.sprite:setUpdatesEnabled(flag) and playdate.graphics.sprite:updatesEnabled() seem to be the same entry.)

Assuming I understand correctly, for the A button case:

  1. In playdate.update(), you notice the A button has been pressed and set carRolling to false.
  2. In your sprite's update() method, you see that carRolling is false and set updatesEnabled to false.
  3. Now your car sprite's update() won't be called again.

If that's all correct, do you see the problem? The code to re-enable updates for your sprite is inside sprite:update(), but that method is no longer being called because updates are disabled. You'll have to move that logic somewhere else for it to work.

1 Like

Thank so much. I realized it was much easier to simply save the last frame of animation before the stop button is pressed and let the sprite update continue to run with that saved image and when the go button is pressed, restart reloading images . Makes for easier logic.

Thanks so much for the assist.