Any way to ensure timer.performWithDelay will execute?

I have found that if I have some enterFrame update loop in my game that prints out debug text nonstop (not a good idea of course), but at the same time I run a function which has nested functions that run based on timer.performWithDelays, in some occasions those nested functions don’t get executed.

For example:

function someFunction()

        player.sprite:setSequence( “startSpinning” )

        player.sprite:play()

        local function speedUp()

            player.sprite.timeScale = player.sprite.timeScale*2

        end

        local function speedDown()

            player.sprite.timeScale = player.sprite.timeScale/2

        end

        local function spinFast()

            player.sprite:setSequence( “spinFast” )

            player.sprite:play()

        end

        local function slowDown()

            player.sprite:setSequence( “spinSlow” )

            player.sprite:play()

        end

        timer.performWithDelay( 800, spinFast )

        timer.performWithDelay( 1600, speedUp )

        timer.performWithDelay( 2000, speedUp )

        timer.performWithDelay( 2400, speedUp )

        timer.performWithDelay( 3900, speedDown )

        timer.performWithDelay( 4300, speedDown )

        timer.performWithDelay( 4700, speedDown )

        timer.performWithDelay( 5100, slowDown )

  

end    

I’m basically trying to make a sprite in game spin faster and slower on certain time intervals. There might be other alternatives to making this happen without incessantly using timer.performWithDelay(), but is there a better way to ensure they’ll all get run? I’ve had cases where the first “speedUp” call doesn’t run and nothing runs afterwards…

Can some other code in enterFrame that runs nonstop make some of the timer.performWithDelay() calls become ignored? 

I’ve never had a timer.performWithDelay not run.  They always run.

How are you determining that they’re not running?  Just by looking at the speed of the sprite?  That’s not a good test because the gap between your timers is pretty short, only 400ms, so you might simply not notice the change, and the change itself may not be that big.  Instead, you could just put some print statements in the timer.performWithDelays to verify that they’re executing.

If it appears like it’s not running, my guess of what’s happening is this: The debug statements being printed every frame are actually slowing down your frame rate (surprisingly, lots of print statements does noticeably slow down execution).  As a result, the timers are getting “backed up” and executing at a shorter interval than 400ms in an attempt to catch up.

  • Andrew

I’ve never had a timer.performWithDelay not run.  They always run.

How are you determining that they’re not running?  Just by looking at the speed of the sprite?  That’s not a good test because the gap between your timers is pretty short, only 400ms, so you might simply not notice the change, and the change itself may not be that big.  Instead, you could just put some print statements in the timer.performWithDelays to verify that they’re executing.

If it appears like it’s not running, my guess of what’s happening is this: The debug statements being printed every frame are actually slowing down your frame rate (surprisingly, lots of print statements does noticeably slow down execution).  As a result, the timers are getting “backed up” and executing at a shorter interval than 400ms in an attempt to catch up.

  • Andrew