Hello :) if you want test an AS2 EventDispatcher implementation based on the W3C event model DOM 2/3 (the AS3 event model is based on the same dom).... you can use my personal event model in my framework VEGAS :
- http://vegas.riaforge.org/ - http://osflash.org/vegas Install my framework in Flash with the classpath AS2/trunk/src in the project directory all examples in the AS2/trunk/bin/test/vegas/events directory of the project : http://svn.riaforge.org/vegas/AS2/trunk/bin/test/vegas/events/ My vegas.events.* package use 3 interfaces ( EventTarget, EventListener and Event ) -------------- Exemple 1 (Use vegas.events.Delegate class) : import vegas.events.BasicEvent ; import vegas.events.Delegate ; import vegas.events.Event ; import vegas.events.EventDispatcher ; import vegas.events.EventListener ; var debug = function(ev:Event) { trace("debug :: " + this + " : " + ev.getType()) ; } var dispatcher:EventDispatcher = new EventDispatcher() ; dispatcher.addEventListener("onTest", new Delegate(this, debug)) ; dspatcher.dispatchEvent( new BasicEvent("onTest", this, "Hello World") ) ; -------------- Exemple 2 : Use your customs Events and EventListener : # The EventListener import vegas.events.Event ; import vegas.events.EventListener; class test.events.DebugHandler implements EventListener { /** * Creates a new DebugHandler instance. */ public function DebugHandler(name) { _name = name || "noName" ; } public function getName():String { return _name ; } /** * Handles the events. * @see EventListener implementation ! */ public function handleEvent(e:Event) { trace("-------- DebugHandler : Event has been triggered."); trace("Event type : " + e.getType()) ; trace("Event target : " + e.getTarget()) ; trace("Event context : " + e.getContext()) ; trace("Event in queue : " + e.isQueued()) ; } public function toString():String { return "<DebugHandler>" + _name + "</DebugHandler>" ; } private var _name:String ; } # The test in Flash : import vegas.events.BasicEvent ; import vegas.events.DynamicEvent ; import vegas.events.Event ; import vegas.events.EventDispatcher ; import vegas.events.EventListener ; import test.events.DebugHandler ; var listener:EventListener = new DebugHandler(); var d:EventDispatcher = EventDispatcher.getInstance("myDispatcher") ; d.addEventListener("onTest", listener) ; // register the EventListener d.dispatchEvent( new DynamicEvent("onTest", "myTarget", "myContext") ) ; // Dispatch an Event -------------- Exemple 3 : Use a 'Priority" with your addEventListener method import vegas.events.Event ; import vegas.events.EventDispatcher ; import vegas.events.EventListener ; import vegas.events.Delegate ; function debug( ev:Event, msg:String ):Void { trace ("--------") ; trace ("message -> " + (msg || "empty")) ; trace ("type : " + ev.getType()) ; trace ("target : " + ev.getTarget()) ; trace ("context : " + ev.getContext()) ; trace ("this : " + this) ; } var listener1:EventListener = new Delegate(this, debug, "listener1") ; var listener2:EventListener = new Delegate(this, debug, "listener2") ; var listener3:EventListener = new Delegate(this, debug, "listener3") ; var listener4:EventListener = new Delegate(this, debug, "listener4") ; var dispatcher:EventDispatcher = new EventDispatcher() ; dispatcher.addEventListener("onTest", listener1, false, 3) ; dispatcher.addEventListener("onTest", listener2, false, 1) ; dispatcher.addEventListener("onTest", listener3, false, 2) ; dispatcher.addEventListener("onTest", listener4, false, 0) ; var time:Number ; var loop:Number = 1 ; Key.addListener(this) ; onKeyDown = function() { time = getTimer() ; for (var i:Number = 0 ; i<loop ; i++) { dispatcher.dispatchEvent( { type : "onTest", target : this } ) ; } time = getTimer() - time ; trace ("test : " + time + "ms") ; } -------------- Exemple 4- : AutoRemove your EventListener after the first handle of the event ! import vegas.events.BasicEvent ; import vegas.events.Event ; import vegas.events.EventDispatcher ; import vegas.events.EventListener ; import test.events.DebugHandler ; var debug:EventListener = new DebugHandler(); // use the class in the example 2 ! var dispatcher:EventDispatcher = new EventDispatcher() ; var myEvent:Event = new BasicEvent("onTest", this, "context") ; dispatcher.addEventListener("onTest", debug, false, null, true) ; // autoRemove = true dispatcher.dispatchEvent( myEvent ) ; dispatcher.dispatchEvent( myEvent ) ; // autoRemove the listener you can't handles the event now... .... etc... you can use bubbling and capturing event with my EventDispatcher class ... Use a global EventDispatcher singleton with the static getInstance() method... use the global propagation of the events with the addGlobalEventListener method etc. etc... You can use to finish the FrontController class of my framework to centralize all your events with a global propagation of the events : import vegas.events.BasicEvent ; import vegas.events.Delegate ; import vegas.events.Event ; import vegas.events.EventDispatcher ; import vegas.events.FrontController ; var action:Function = function (ev:Event) { trace("> " + this + " :: " + ev.getType() + " - " + ev.getTarget()) ; } var listener = {} ; listener.handleEvent = function(ev:Event):Void { trace("> " + this + " :: " + ev.getType() + " - " + ev.getTarget()) ; } listener.toString = function () { return "[MY_LISTENER]" ; } var controller:FrontController = new FrontController( ) ; controller.insert("MY_EVENT1", new Delegate(this, action)) ; controller.insert("MY_EVENT2", listener) ; // Test EventDispatcher.getInstance().dispatchEvent( new BasicEvent("MY_EVENT1", this) ) ; trace("---") ; EventDispatcher.getInstance().dispatchEvent( new BasicEvent("MY_EVENT2", this) ) ; For me it's the best solution to use an event model AS2 like AS3 ! EKA+ :) 2007/4/3, David Ngo <[EMAIL PROTECTED]>:
You'll need a reference of the dispatching class in your listener class to receive events: // broadcasting class import mx.events.EventDispatcher; class EventClass { // EventDispatcher methods public var addEventListener:Function; public var removeEventListener:Function; private var dispatchEvent:Function; public function EventClass() { } public function onInit():Void { trace('onInit fired!'); dispatchEvent({type: "onInit", target: this}); } } // listener class import mx.utils.Delegate; class ListenerClass { public function ListenerClass(eventClass:MyEventClass) { eventClass.addEventListener("onInit", Delegate.create (this, onInitHandler)); } private function onInitHandler(event:Object):Void { trace('onInitHandler invoked'); } } // implementation var eventClass:EventClass = new EventClass(); var listenerClass:ListenerClass = new ListenerClass(eventClass); eventClass.onInit(); -----Original Message----- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] On Behalf Of Merrill, Jason Sent: Tuesday, April 03, 2007 9:47 AM To: flashcoders@chattyfig.figleaf.com Subject: RE: [Flashcoders] Events for custom classes? Actually, I had been meaning to post again because I had lost my code for getting EventDispatcher working between two classes. Someone posted a response here and I lost it, as well as my code that was working. if anyone can post again on how to get one class to dispatch a CUSTOM event and get another CUSTOM class to listen to that event, please post! Helmut and I would like to see an example. The help docs on EventDispatcher are really awful, and I haven't seen a lot of stuff online. Jason Merrill Bank of America GT&O Learning & Leadership Development eTools & Multimedia Team >>-----Original Message----- >>From: [EMAIL PROTECTED] >>[mailto:[EMAIL PROTECTED] On Behalf >>Of Helmut Granda >>Sent: Tuesday, April 03, 2007 12:35 PM >>To: Flashcoders mailing list >>Subject: Re: [Flashcoders] Events for custom classes? >> >>On 4/3/07, Helmut Granda <[EMAIL PROTECTED]> wrote: >>> >>> Jason, >>> >>> Were you able to figure this out exactly the way you wanted it? >>> >>> -h >>> >>> On 2/16/07, Merrill, Jason < [EMAIL PROTECTED]> wrote: >>> > >>> > Ah - nevermind - figured out I had removed the scope to my >>> > webservice and forgot to put it back in. Delegate works >>fine for me >>> > now, sorry about the noise, and thanks so much everyone >>for the help!! >>> > >>> > Jason Merrill >>> > Bank of America >>> > Learning & Organizational Effectiveness >>> > >>> > >>> > >>> > >>> > >>> > >>> > >>_______________________________________________ >>Flashcoders@chattyfig.figleaf.com >>To change your subscription options or search the archive: >>http://chattyfig.figleaf.com/mailman/listinfo/flashcoders >> >>Brought to you by Fig Leaf Software >>Premier Authorized Adobe Consulting and Training >>http://www.figleaf.com http://training.figleaf.com >> _______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com _______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com
_______________________________________________ Flashcoders@chattyfig.figleaf.com To change your subscription options or search the archive: http://chattyfig.figleaf.com/mailman/listinfo/flashcoders Brought to you by Fig Leaf Software Premier Authorized Adobe Consulting and Training http://www.figleaf.com http://training.figleaf.com