Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

Send custom events with EventDispatcher
Started by qwidave Aug 19 2014 10:37 PM

* * * * * 1 votes
2 replies to this topic
event dispatcher eventdispatcher listener oop custom event objects
[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

qwidave

[GLOBAL: userInfoPane.html]
qwidave
  • Pro
  • PipPipPip
  • 32 posts
  • Advanced Member

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


[TOPIC: post.html]
#2

develephant

[GLOBAL: userInfoPane.html]
develephant
  • Pro
  • PipPipPipPipPipPip
  • 423 posts
  • Jedi

As a former Flash developer, this is awesome!

 

Thanks.



[TOPIC: post.html]
#3

mroberti

[GLOBAL: userInfoPane.html]
mroberti
  • Enterprise
  • PipPipPipPipPipPip
  • 271 posts
  • Jedi

Dave this is awesome!! Who needs FSMs when you got event-driven stuff?! I can't wait to really play around with this!!

 

-Mario




[topic_controls]
[/topic_controls]



Also tagged with one or more of these keywords: event, dispatcher, eventdispatcher, listener, oop, custom event, objects