implement remove event lua function

This commit is contained in:
Eduardo Bart 2011-11-06 22:14:51 -02:00
parent 4e0ca24cb7
commit 5b1b170165
2 changed files with 28 additions and 15 deletions

View File

@ -1,28 +1,40 @@
local eventId = 1
local events = { }
local eventId = 0
local eventsTable = { }
local orig = { scheduleEvent = scheduleEvent,
addEvent = addEvent }
-- fix original scheduleEvent
function scheduleEvent(func, delay)
eventId = eventId + 1
local id = eventId + 1
local function proxyFunc()
eventId = eventId + 1
local id = eventId
local function proxyFunc()
if eventsTable[id] then
func()
table[id] = nil
end
table[id] = proxyFunc
orig.scheduleEvent(proxyFunc, delay)
eventsTable[id] = nil
end
end
eventsTable[id] = proxyFunc
orig.scheduleEvent(proxyFunc, delay)
return id
end
function removeEvent(id)
if id and eventsTable[id] then
eventsTable[id] = nil
end
end
-- fix original addEvent
function addEvent(func)
eventId = eventId + 1
local id = eventId + 1
local id = eventId
local function proxyFunc()
func()
table[id] = nil
if eventsTable[id] then
func()
eventsTable[id] = nil
end
end
table[id] = proxyFunc
eventsTable[id] = proxyFunc
orig.addEvent(proxyFunc)
return id
end

View File

@ -18,6 +18,7 @@ local messageTypes = {
{ type = 'MessageBlue', color = '#3264C8', showOnConsole = true, showOnWindow = false },
{ type = 'MessageRed', color = '#C83200', showOnConsole = true, showOnWindow = false }
}
local hideEvent
-- public functions
function TextMessage.create()
@ -47,11 +48,11 @@ function Game.onTextMessage(type, message)
label:setVisible(true)
label:setForegroundColor(messageType.color)
label:setText(message)
label:setStyle(messageType.windowLocation)
time = #message * 75
scheduleEvent(function()
removeEvent(hideEvent)
hideEvent = scheduleEvent(function()
label:setVisible(false)
end, time)
end