I have been using this class for all my event needs. Simply import the
class, then to use:
EventManager.getInstance().addListener("eventDoThis", this); //adds the
listener to the class ('this' is the scope)
EventManager.getInstance().removeListener("eventDoThis", this); //removes
the listener
EventManager.getInstance().dispatch({type:" eventDoThis", param:true,
param2:false, param3:"etc"}); //sends the event with params
private function eventDoThis(eventObj:Object):Void
{
trace("eventDoThis called");
var p1:Boolean = eventObj.param;
var p2:Boolean = eventObj.param2;
var p3:String = eventObj.param3;
}
For me, this setup is nice and clean to use. Just be sure to keep it as a
Singleton to prevent additional EventManagers from overriding this one.
***************CODE*********************
//EventManager.as
import com.core.events.*;
class com.core.events.EventManager {
private var eventListeners:Object = new Object();
private static var _instance:EventManager;
private function EventManager(Void){}
public static function getInstance(Void):EventManager
{
if(_instance === undefined){
_instance = new EventManager();
}
return _instance;
}
//dispatch broadcasts the event to all who are setup as listeners
public function dispatch(eventObject) {
var tmpA:Array = eventListeners[eventObject.type];
// Loop through this way to avoid problems with adding/removing
listeners during the dispatch
for (var i in tmpA) {
tmpA[i][eventObject.type](eventObject);
}
}
//add a listener to the queue, this will get called once a dispatch
event happens that matches
public function addListener(event:String, listener:Object) {
if (eventListeners[event] == undefined) {
eventListeners[event] = new Array();
}
removeListener(event, listener);
// a listener should only listen to an event once
eventListeners[event].push(listener);
}
//remove the listener from the queue, this prevents additional
rebroadcasting.
public function removeListener(event:String, listener:Object) {
var tmpA:Array = eventListeners[event];
var len:Number = tmpA.length;
for (var i:Number = 0; i<len; i++) {
if (tmpA[i] == listener) {
tmpA.splice(i, 1);
return;
}
}
}
}
***************END CODE*********************
I hope this helps you out, let me know if you have any problems with it or
have any additional questions.
Steve
-----Original Message-----
From: [EMAIL PROTECTED] [mailto:
[EMAIL PROTECTED] On Behalf Of Sean Scott
Sent: Tuesday, September 26, 2006 2:09 PM
To: [email protected]
Subject: [Flashcoders] Delegating Events and AS2
Hi All!,
wondering if someone can point me in the right direction. I am trying
to find a ASBoradcast / Event Dispatcher light model for my app.
Basically i have a number of MCs that will have to either react to
events being broadcast or broadcast their own.
I have Essential AS2 by Colin Moock. Trying to find something i can
import and maybe pass scope to it, vs have my main class extend it.
I've googled, searched the archived and exausted my more talented
flash developer friends.
Thanks,
Sean
_______________________________________________
[email protected]
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
_______________________________________________
[email protected]
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