Basic Timers Versus Dedicated Alarm Classes


(Alex Larioza) #1

This following isn’t directed towards FlashPunk in particular, but is rather a general programming question.

Usually when I need to wait a certain amount of time before doing something, I use a simple if-statement to track the elapsed time:

If (timer > 0)
{
    timer -= FP.elapsed
}
else
{
    timer = 100 // reset timer
    // ... do something
}

Is there any benefit of creating a dedicated class to handle timers (either an existing class such as FP’s Alarms or a custom class)? With FP’s alarms, I’d imagine it takes slightly more resources as the timer needs to be added to the tween list, then updated each frame. You’d also be called the “New” operator to create a new object versus creating a single Number variable. Thoughts?


(Jacob Albano) #2

Tweens are managed with linked lists, which are much faster to iterate and modify than native as3 arrays. The only overhead you’d be dealing with is when the Alarm object is created, and if you cache it or set it to LOOPING than you can pretty much forget about that as well.

If you manage adding/updating/removing your timers in a performant way (which Flashpunk definitely does), worrying about this type of thing is an over-optimization.