elapsed field on enterFrame event tables

I am very much more concerned in my projects with the passage of time than with the current clock. Presently, this obliges me to track the last time my listeners were triggered externally, and calculate the difference to subtract it from another persistent value that indicates how much more time I have to wait. (Storing next-fire times instead of remaining waits is awkward as well.)

It would save me a lot of time to receive the time elapsed since the last enterFrame dispatch as an additional field on the event table. Alternatively, an API to get the current frame rate might suffice. I can also implement this function myself given two things I am not currently certain of; that listeners are fired in the order they were added, and that the table passed to listeners as the event argument is reused. In that case I could save

1
2
3
4
5
6
local tPrime = system.getTimer()
Runtime:addEventListener('enterFrame',
    function(event)
        event.elapsed, tPrime = event.time - tPrime, event.time
    end
)

Well the second question is easy to answer. Yes, it's the same table each time, not like a new copy of the table is initialized each time. Try running this code:

1
2
3
4
5
6
7
local table = {}
table.count = 0
function table:enterFrame(event)
        self.count = self.count + 1
        print(self.count)
end
Runtime:addEventListener("enterFrame", table)

I have, as you point out, confirmed the second point. The first point did work for my initial test, but I don't know if this is a reliable behavior or not.

views:1838 update:2011/9/18 20:20:08
corona forums © 2003-2011