In order to broadcast messages to event listeners, the event dispatcher is limited to either the global Runtime object or display objects. This limitation places messaging in the two scopes instead of between objects that are supposed to be talking to each other. This just doesn’t feel right to me; so I’m releasing yet another EventDispatcher, perhaps other developers may find it useful too.
Those who came from the good old Flash 5 days may remember FLEM (FLash Event Model). It was the first listener event model for Flash and ActionScript, and was created by Branden Hall and further developed and maintained by me. I’ve adapted the basic event model mechanism found in ActionScript 2/3 to Lua. This EventDispatcher has a similar API as the listener model in Corona SDK, with some extra features thrown in (such as optional extra parameters when dispatching events, and returning status).
EventDispatcher provides custom event broadcaster/listener mechanism to regular Lua objects, it works as regular Lua 5.1 / 5.2 code, in Corona SDK, and likely other Lua-based frameworks.
Basic usage:
local EvtD = require "EventDispatcher" local listener = { eventName = function(event) print(event.name) end } local broadcaster = EvtD() broadcaster:addEventListener( "eventName", listener ) broadcaster:hasEventListener( "eventName", listener ) broadcaster:dispatchEvent( { name="eventName" } ) broadcaster:removeEventListener( "eventName", listener )
More info here: http://swfoo.com/?p=632
Enjoy!
Dave Yang - Quantumwave Interactive Inc.
http://qwmobile.com
http://swfoo.com