Easy way to reverse animation loops!

Hello fellow PlayDate developers!

I found a simple and cheap way to reverse the playback of animations. In my specific case, I have an elevator that I animated going down, and didn't want to duplicate the frames in memory to reverse the playback for when the elevator is going up. The obvious "hack" would be to set the .step value on your playdate.animation.loop to -1, ala:

tableElevatorUpFrames.step = -1

This doesn't work however, as it throws an error "playdate.graphics.animation.loop.step must be a positive integer".

Thinking about it a bit, and knowing that I have 6 frames of animation, I thought I'd try something sneaky and set the step to 5, and shouldLoop = true. This way it's playing "forward" but always skipping ahead and looping to the previous frame.

tmp = gfx.imagetable.new("images/ElevatorShaft") -- Loads 6 images into an animation table
	tableElevatorDownFrames = gfx.animation.loop.new(200, nil, true) -- shoudLoop = true 
	tableElevatorDownFrames:setImageTable(tmp)
	tableElevatorUpFrames = gfx.animation.loop.new(200, nil, true) -- shoudLoop = true 
	tableElevatorUpFrames:setImageTable(tmp)
	tableElevatorUpFrames.startFrame = 1
	tableElevatorUpFrames.endFrame = 6
	tableElevatorUpFrames.step = 5			
	tableElevatorUpFrames.shouldLoop = true

This actually works perfectly and I didn't have to duplicate the imagetable.

I hope this nugget helps someone else, happy coding!
Sam

8 Likes

Very cool lateral thinking!

1 Like

this could half the required storage space for symmetrical animations, and is generalizable to a function even. great discovery

1 Like