I wanted to set a sprite to be destroyed after it's animation plays, so I set a timer to start the same time as the animation with the same length (i know there's a function for checking if an animation has ended but the object is created on button press so using that in the update function would be out of scope of the object itself), yet the timer sends the debug print message the same instant that the button press starting it occurs?
below is the code for the class:
import "CoreLibs/object"
import "CoreLibs/graphics"
import "CoreLibs/sprites"
import "CoreLibs/timer"
import "CoreLibs/animation"
class('decide').extends(playdate.graphics.sprite)
local geo = playdate.geometry
local Animator = playdate.graphics.animator
local ANIMATION_TIME = 2000
function decide:init()
decide.super.init(self)
self.thumb = playdate.graphics.image.new("img/thumbUp")
self:setImage(self.thumb)
self:setZIndex(1000)
self:setCenter(0.5, 1) -- set center point to center bottom middle
self:setCollideRect(2,0,16-4,32)
self:moveTo(-20,120)
self:add()
local thumbls = geo.lineSegment.new(-20, 120, 420, 120)
local thumblsAnim = Animator.new(ANIMATION_TIME, thumbls, playdate.easingFunctions.outInQuad, 0)
self:setAnimator(thumblsAnim, false, false)
--timer uses same time variable as the animator, calls destroy function
playdate.timer.new(ANIMATION_TIME, self:destroy())
end
function decide:update()
--this won't work since main update is out of scope of created object
decide.super.update(self)
if Animator:ended() then
print("anim ended")
end
end
function decide:destroy()
--should print when animation ends, instead prints when object created
print("destroyed")
end