How to Use AsBroadcaster object

The ASBroadcaster object has four methods, I will introduce them in details.


Code:
static function initialize(o:Object);
//This is a static method, meaning to create a _listener, which is used to store all listener objects.
//Add each functions as event source for o. Then what is the function of event source?

static function broadcastMessage(msg:String);
//Message;
//This method is broadcast method; one of the key functions of event source is to broadcast a message saying "Something happened!" to public. 

static function addListener(o:Object);
//Login listener method, in another word, to decide whom the message is sent to.

static function removeListener(o£ºObject);
//Logout listener method that means the message is not sent to anyone any more.


It seems that AsBroadcaster object is just a little bit complex. For help with understanding it, I will give you some examples:



Code:
  //-------------------Initialized Part ------------------------------------
  var headquarters=new Object();
  //Headquarters is a place to send message, we call it event source.
  AsBroadcaster.initialize(headquarters);
  //call static method "initialize" of AsBroadcaster object to add each functions as event source to headquarters.
  var soldier=new Object();
  //generate soldier object.
  soldier.onAttack=function(){
  trace("soldier will attack
  //the action the soldier performs when he receives "onAttack" message.
  }
  soldier.onGuard=function(){
  trace("soldier guard here!");
  }
  headquarters.addListener(soldier);
  //Insert soldier as listener of headquarters, he must comply to headquarters’ commands from now on.
  var armored=new Object();
  armored.onAttack=function(){
  trace("armored will atack!");
  }
  armored.onGuard=function(){
  trace("armored guard here!");
  }
  headquarters.addListener(armored);
  var artilleryman=new Object();
  artilleryman.onAttack=function(){
  trace("artilleryman will attack!");
  }
  artilleryman.onGuard=function(){
  trace("artilleryman guard here!");
  }
  headquarters.addListener(artilleryman);
  //-------------------Demonstrate as Follows------------------------------------
  trace("headquarters’ first command");
  headquarters.broadcastMessage("onAttack");
  //Headquarters use "broadcastMessage" method to broadcast "onAttack" command.
  trace("headquarters’ second command");
  headquarters.removeListener(soldier);
  //Headquarters use "removelistener" method to logout the obligation that soldier has to follow commands.
  headquarters.broadcastMessage("onGuard");
  //Headquarters use "broadcastMessage" method to broadcast "onGuard" command. Notice: Soldier can not receive this command right now.

OK, test it now!

Now, let us see the limits of AsBroadcaster here:
1. How to broadcast the message "Attack NO.1 target." sent out by headquarters? We certainly can custom an "onAttackNO.1target" method for soldier and then listen the message "onAttackN.1target" broadcasted by headquarters, but will you custom numbers of similar commands if a great number of commands needed to be sent? How can you write the targets if they are produced randomly? How can the soldier distinct the different command senders and perform the different actions while he is probable to listen broadcast from "Command Post"? Absolutely, soldier can not make sure message "onAttack" is from headquarters or is from "Command Post". Therefore, the first limit of "Asbrodcaster" is that it can not give any specification or any parameters though it broadcasts a message.

2. We take two event sources called "headquarters" and "command Post" as an example. Suppose that we want to give command power to "Command Post" and give defend power to "headquarters", for that headquarters can command attack and defend actions of armored and artilleryman, they can send message "onAttack", something unexpected happens: Soldier attacks at the same time.
To avoid this error, headquarters have to "removeListener(soldier)" before attacking performance. Besides, they have to "addListener(soldier)" after broadcasting message "onAttack". Is it complex? Now we get another limit of "Asbrodcaster": It doesn’t care about which event the listener is listening though it logins a listener.

3. The last limit is come from the second one that is "Asbrodcaster" object can sometime make work inefficient. Suppose that there is another airman and it has "onAirAttack" event method which follows headquarters’ command. It is absolutely a waste of time that soldier, armored and artilleryman try to execute "onAirAttack" event method when headquarters broadcaster "onAirAttack" message. 


Glad to share the time together. Hopefully it is helpfull to you. Any time, good luck!

Back to Top >>

Home

Sothink is a trademark of SourceTec Software Co., LTD.
Sothink Software: SWF Decompiler Flash to HTML5 Logo Maker Logo Design Software Mac Decompiler Video Converter

1234