I have a player sprite walk cycle using an imagetable
and an animation.loop
. I am adjusting \animation.loop.delay
on update() based on the player's speed. However I am getting unexpected results. The apparent animation speed when delay is set to certain values on update does not match the animation speed if I set the animation delay to the same values statically.
I've put together a minimum example which shows this behavior. The three characters on the bottom are animated using a fixed delay.
The character at the top has its animation.delay
set every frame using a sin wave. The line and text help to visualize the delay changing.
Note that what the top animation looks like at various values, e.g. min (200ms) and max (600ms) values, does not match the static reference animations on the bottom.
Example:
https://gloryfish.s3.amazonaws.com/games/adventure/animation-delay-issue.zip
Code for main.lua (images are in the ZIP above).
import 'CoreLibs/animation'
import 'CoreLibs/graphics'
import 'CoreLibs/math'
local gfx <const> = playdate.graphics
local imagetable
local animDynamic
local animStatic200
local animStatic400
local animStatic600
function loadGame()
imagetable, error = gfx.imagetable.new('images/player-walk-right')
assert(imagetable, error)
animDynamic = gfx.animation.loop.new(200, imagetable)
animStatic200 = gfx.animation.loop.new(200, imagetable)
animStatic400 = gfx.animation.loop.new(400, imagetable)
animStatic600 = gfx.animation.loop.new(600, imagetable)
end
function playdate.update()
gfx.clear()
-- Setting `delay` every frame produces unexpected results
animDynamic.delay = playdate.math.lerp(200, 600, (math.sin(playdate.getCurrentTimeMilliseconds() * 0.0005) + 1) / 2)
gfx.drawRect(30, 80, animDynamic.delay / 2, 2)
gfx.drawText(math.floor(animDynamic.delay)..'ms', 30, 90)
animDynamic:draw(30, 30)
animStatic200:draw(30, 120)
animStatic400:draw(100, 120)
animStatic600:draw(170, 120)
gfx.drawText('200ms', 30, 160)
gfx.drawText('400ms', 100, 160)
gfx.drawText('600ms', 170, 160)
end
loadGame()