Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Enter Frame functions manager with tags
local _M = {}
function _M.nextFrame(f)
timer.performWithDelay(1, f)
end
function _M.enterFrame()
for i = 1, #_M.enterFrameFunctions do
_M.enterFrameFunctions[i][1]()
end
end
function _M.eachFrame(f, tag)
if not _M.enterFrameFunctions then
_M.enterFrameFunctions = {}
Runtime:addEventListener('enterFrame', _M.enterFrame)
end
table.insert(_M.enterFrameFunctions, {f, tag})
return f
end
function _M.eachFrameRemove(f)
if not f or not _M.enterFrameFunctions then return end
local ind
for i = 1, #_M.enterFrameFunctions do
if _M.enterFrameFunctions[i][1] == f then
ind = i
break
end
end
if ind then
table.remove(_M.enterFrameFunctions, ind)
if #_M.enterFrameFunctions == 0 then
Runtime:removeEventListener('enterFrame', _M.enterFrame)
_M.enterFrameFunctions = nil
end
end
end
function _M.printEnterFrameFunctions()
if not _M.enterFrameFunctions then return end
for i = 1, #_M.enterFrameFunctions do
print('f =', _M.enterFrameFunctions[i][1], 'tag =', _M.enterFrameFunctions[i][2])
end
end
return _M
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment