Game Development Reference
In-Depth Information
Timer.delayedCall(1000,
function(msg)
print("Hello "..msg)
end,
"Gideros")
Custom Events
You can also create your own events that can be dispatched and set up a listener for these custom
events, as follows:
local theTimer = Timer.new(1000, 5)
function(event)
theTimer:setRepeatCount(10)
if theTimer:getCurrentCount() == 4 then
evt = Event.new("hi")
stage:dispatchEvent(evt)
end
end)
function(event)
print("All timer loops completed")
end)
theTimer:start()
stage:addEventListener("hi",
function()
print("Hello")
end)
The way to manage custom events is to set up an event listener for a particular type of event using
addEventListener . After creating an event using the Event class with Event.new(eventName) , when
we want to raise the event, we can use the dispatchEvent function.
Removing Events
Events can be removed using removeEventListener in a manner similar to how they're added using
addEventListener . To add an event handler, you can use the following:
object:addEventListener(EventName, evtHandler)
Similarly, here's how to remove an event handler:
object:removeEventListener(EventName, evtHandler)
 
Search WWH ::




Custom Search