EventAPI with usage example
//afEventDispatcher is an event api. It will dispatch to a function anywhere and everywhere you have attached a listener for the afDispatcher.AF_EVENT.
//The afEventDispatcher has these two following public methods:
//.getInstance(this); Gets an instance of the of the afEventDispatcher
//.afEvent(object); Requires an argument of type Object; to pass messages to the dispatcher.
//The afEventDispatcher has one type of event:
//.AF_EVENT has the normal event properties, with the exception of the special property "afArgument." The afArgument is the object argument sent using .afEvent(object); and contains all of the objects properties.
// afEventDispatcher has two parts. The best way to try it out out is to copy both parts.
// First set up your file to receiving messages.
// Second set up your file to send a message.
//USE CASE NUMBER ONE (RECEIVING)
//In order to receive the eventObject, follow these four setup steps:
//First, point to dispatcher_AF.swc in your library path and import it.
import com.aubrey.eventDispatcherAFArgs.afEventDispatcher;
//Second, create a afEventDispatcher variable, and instantiate it.
var afDispatcher:afEventDispatcher = afEventDispatcher.getInstance(this);
//Third, attach a listener for afDispatcher.AF_EVENT
afDispatcher.addEventListener(afDispatcher.AF_EVENT, af_Event_received, false,0,true);
//Forth, write the function that will respond to the afDispatcher.AF_EVENT argument.
//This function will listen for the afDispatcher.afEvent(theObj);.
function af_Event_received(e):void{
// Here you see theObj being read in as e.afArgument.
trace(this.name +" af_Event_received: (afArgument.isType = "+e.afArgument.isType+")");
switch(e.afArgument.isType){
case "example":
trace(e.afArgument.myValue);
break;
}
}
//place the ^above lines of code onto any timeline.
//USE CASE NUMBER TWO (SENDING)
//In order to send the an eventObject, you need to follow these three setup steps:
//Use the below line to dispatch the AF_EVENT with an argument object.
//First, point dispatcher_AF.swc in your library path and import it.
import com.aubrey.eventDispatcherAFArgs.afEventDispatcher;
//Second, create a afEventDispatcher variable, and instantiate it.
var afDispatcher:afEventDispatcher = afEventDispatcher.getInstance(this);
//Third, write a function that puts afDispatcher to use. The function below will dispatch an afDispatcher.AF_EVENT with an argument object.
function myExample():void{
trace("myExample");
var theObj:Object = new Object();
theObj.isType = "example";
theObj.myValue = Math.random() +" is myValue";
// theObj gets passed into the af_Event_received as an AF_EVENT.
afDispatcher.afEvent(theObj);
}
myExample();