Event Broadcaster – passing events from one object to another
let’s imagine this scenario:
we instantiate a certain number of instances of object A on the stage (_root) .. and we want each object to be receive a different value, and have that object do something with it
a real life example would be: create a client object and pass the client name as an argument to that object.
and let’s assume that we have a complex class hierarchy that requires code optimization, which generally means that we need to know how to find the code without scrolling through a 5 page long classes.
for that, we can create an Event Manager class that listens to events from all objects in the structure and handles the calls and manages the data.
a simple example would be:
import EventManager.*;
class caller{
function caller(message:String){
EventManager.getInstance().setDataChange(message);
}
}
which will trigger an event-change data event on creation.
this class imports a static class called EventManager which catches the dispatched event and based on its type handles the passed Data.
so this class looks like this:
class EventManager {static var _EM:EventManager ;
static var broadcaster:Object
var listener:Object;
function EventManager(){
broadcaster = new Object();
AsBroadcaster.initialize(broadcaster);
listener = new Object();
listener.onSendEevent = handleEvent;
broadcaster.addListener(listener);
}
static function getInstance (Void): EventManager {
if (_EM == null) _EM = new EventManager();
return _EM;
}
private function handleEvent(message){
// do somethig with the Data
trace("Recieved Message: "+message)
}
public function setDataChange(message:String){
broadcaster.broadcastMessage("onSendEevent" , message);
}
}
and finally, on the main class (where objects are created) this is the final code:
import caller.*;
for (var i:Number=0; i<10; 0++){
this["callerObject"+i] = new caller("Object Name:"+i);
}
so everytime an object is created, the caller object fires an event that carries the object name as content. once fired, the EventManager will be listening to this event, and will collect the name of the object and does something with it (here it traces the name, but in other cases we will most probably save the names to databases or add them to array, etc…)
so the output of these lines of code would be:
Recieved Message: Object Name 0
Recieved Message: Object Name 1
Recieved Message: Object Name 2
Recieved Message: Object Name 3
Recieved Message: Object Name 4
Recieved Message: Object Name 5
Recieved Message: Object Name 6
Recieved Message: Object Name 7
Recieved Message: Object Name 8
Recieved Message: Object Name 9
There’s another easy way to do this – static event dispatcher
public static var _dispatcher:EventDispatcher = new EventDispatcher();
public static function addEventListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeekReferance:Boolean = false):void {
_dispatcher.addEventListener(type, listener, useCapture, priority, useWeekReferance);
}
public static function removeEventListener(type:String, listener:Function):void {
_dispatcher.removeEventListener(type, listener, false);
}
public static function hasEventListener(type:String):Boolean {
return _dispatcher.hasEventListener(type);
}
public static function sendEvent(type:String):void {
if(!ArrayTools.has(_dispatched_events, type)){
_dispatched_events.push(type);
}
_dispatcher.dispatchEvent(new Event(type));
}
public static function alreadyDispatched(type:String):Boolean {
return ArrayTools.has(_dispatched_events, type);
}
========================
just snipper from my framework. I can subscribe everything in every part of my code.