Timer going off instantly?

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"


local geo = playdate.geometry

local Animator = playdate.graphics.animator

local ANIMATION_TIME = 2000

function decide:init()



    self.thumb = playdate.graphics.image.new("img/thumbUp")



    self:setCenter(0.5, 1)  -- set center point to center bottom middle




    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())



function decide:update()

    --this won't work since main update is out of scope of created object


    if Animator:ended() then

        print("anim ended")



function decide:destroy()

    --should print when animation ends, instead prints when object created



You should be using performAfterDelay to register the callback instead. The behavior of your timer currently is something that runs for a given duration.

Edit: the documentation is very useful (direct link to timer section): Inside Playdate

seems like using performAfterDelay calls an error about self:destroy() not being a function? odd since timer was able to use it no issue...

Edit: removed my original answer, didn’t notice it was an object.

How are you trying to pass the function? You should be using self.destroy, not self:destroy I believe.

Also, your original function has a bug. You’re executing the function and passing it’s result to the timer init, hence why it fires immediate. You should be passing self.destroy with no parentheses.

ah, i think that fixed it :]

1 Like

It is a little confusing that dot syntax can be an unspecialized reference or a static function call, but an instance (colon) method is only ever used for execution.

1 Like

It does seem to me you should file a bug report though- the second arg of timer.new should always be a function and should be checked and raise an error similarly to the delay function.

Edit: Timer doesn’t raise an error for invalid arguments