Discussion on the Asbroadcaster (broadcast) class

Source: Internet
Author: User
Tags define final

Foreword: The Forum about Asbroadcaster (broadcast) class discussion many, has many exquisite narration, but in order to have a comparison with the Eventdispatcher (Dispatch) class, had to take this "the old man" to make a cameo appearance. To it I will not speak very detailed, puzzled place can go to search the predecessors of the post Oh.

The Asbroadcaster (broadcast) class is written "Asbroadcaster" in MX (FLASH6), which is one of the reasons why the previous program occasionally occurred in version 2004. The Asbroadcaster class has four static methods:

static function Initialize (O:object);
O: Objects
Initialize: Initializes the method, attaching the function of object o as an event source. So what is the function of the event source?
static function Broadcastmessage (msg:string);
MSG: Message
Broadcastmessage: Broadcast method, one of the main functions of the event source is to broadcast a message to the outside world, telling the people listening to the radio, "an incident has happened!"
static function AddListener (O:object);
O: Objects
AddListener: Registering the Listener method, which means deciding who to broadcast the message to.
static function RemoveListener (O:object);
O: Objects
RemoveListener: Logout listener method, that is, the message no longer broadcast to who listen

So it seems that the Asbroadcaster class is not very complex, the following look at its specific use!
-----ASBROADCASTER TEST. FLA start------

var command =new Object ();
Ah, the headquarters is a place to post command messages, and it's a good choice to use it as an event source.
Asbroadcaster.initialize (Headquarters);
Here directly use Asbroadcaster to refer to the Asbroadcaster class, because of the above
Call the static method initialize of the Asbroadcaster class to attach the functions as an event source to the command
var infantry =new Object ();
Generate Infantry Objects
Infantry. On offense =function () {
On offense: This is prefixed with ' on ' to show that this method is a way of responding to a certain event
Trace ("Infantry received a call from Headquarters, travel light, and rushed to the enemy position forward!");
Measures taken after the ' on offense ' message is received
}
Infantry. On Garrison =function () {
Ditto
Trace ("Infantry received command calls, on-site standby, more set sentry, to prevent the enemy!");
}
Headquarters. AddListener (infantry);
Add the infantry to the command's monitor, and from now on he will follow the commands of the command.
The following similarities are not to be listed here
var armored Armor =new Object ();
Armor. On offense =function () {
Trace ("Armored forces received calls from Headquarters, add sufficient horsepower, rushed to enemy positions into!");
}
Armoured troops. On Garrison =function () {
Trace ("Armored forces received calls from Headquarters, on-site standby, overhaul armor, ready to attack at any time!");
}
Headquarters. AddListener (armoured forces);
var artillery =new Object ();
Artillery. On offense =function () {
Trace ("The artillery received a call from the command, to the enemy position violently bombardment!");
}
Artillery. On Garrison =function () {
Trace ("Artillery received command calls, on-site standby, to ensure adequate ammunition preparation!");
}
Headquarters. AddListener (artillery);
-------------------below to start the demo------------------------------------
Trace ("command number One");
Headquarters. Broadcastmessage ("on offense");
Headquarters uses ' Broadcastmessage ' method to broadcast ' on offense ' instruction
Trace ("Command No. Second");
Headquarters. RemoveListener (infantry);
"will be outside, June order has not been granted", the headquarters with ' RemoveListener ' method to write off the ' infantry ' obey the obligation of orders
Headquarters. Broadcastmessage ("on Garrison");
The command was broadcast ' on Garrison ' by the ' broadcastmessage ' method. Note: Infantry is not stationed now.
-----ASBROADCASTER TEST. FLA End-----

The concrete test result, everybody tries to know. Now let's look at the limitations of the Asbroadcaster class:
1. If the command issued by the headquarters is "offensive to a high ground", how should this message be broadcast? We can certainly define an ' on to the Heights ' approach for the infantry, and then monitor the command broadcast ' on the high ground attack ' message. But if you need to send n a ' go to a certain altitude ' command, wouldn't it be to define n a similar event method? If the target of the offense is immediately determined, how should it be written? If the infantry were to monitor the "field command" broadcast at the same time, how would he take different actions according to different commanders? Infantry is not sure whether an ' on offense ' message comes from the command or the ' Field command '. So the first limitation of the Asbroadcaster class is that although it broadcasts a ' message ', it cannot give a specific description of the message.
2, we know that in wartime in order to achieve the strategy of "when the circumstances", often set up a "battlefield command." Let's say we want to give the command of the infantry to the "field command" and give the infantry garrison to "headquarters". Because the headquarters also has the armor and artillery attack and Garrison command, so it can broadcast "on attack" message, but an unexpected thing will happen: Infantry also followed the attack!
To prevent errors, the command had to "RemoveListener" before attacking. But in order to prevent the broadcast "on garrison" message when the infantry no longer stationed, so after the broadcast "on attack" message must again "AddListener (infantry)." How tired is the headquarters? So the second limitation of the Asbroadcaster class is that it registers a listener, but does not care what the listener is listening to.
3. The third limitation of the Asbroadcaster class is actually a second limitation, that is, it can cause inefficient operation in some cases. Suppose there is an air force, it has the "on air Attack" event method, and is under command. When the headquarters broadcast "on air strikes" news, the infantry, armoured forces and artillery will also try to implement the "on air Attack" event method, of course, is a waste of time.

To say that the Asbroadcaster class so many limitations, not that the Asbroadcaster class is not good, but that according to the situation to choose to use different event mechanism. If not related to the above mentioned several cases, with Asbroadcaster class is actually very convenient (more than Eventdispatcher class to save trouble). Also because Asbroadcaster class has so many limitations of the place, just make our protagonist Eventdispatcher class ascended the glorious stage, please see our next story!
Add 1:
The Asbroadcaster.initialize (O) method creates an array of "_listener" for O, which stores all listener objects.
Add 2:
FLASHMX 2004 also has a Mx.transitions.BroadcasterMX class, which is more powerful than the Asbroadcaster feature, with more parameters to support broadcast messages, and more description of the message. Interested friends can visit the special tutorials in my forum [Http://www.wiyiflash.com/bbs]. [Page]
In the previous article we mentioned the limitations of the Asbroadcaster class wrapper as an event source, and now look at how the Eventdispatcher (Dispatch) class expands the functionality of the event source.
The Eventdispatcher class has 2 static methods:

static function Initialize (Object:object): Void
Object: Objects
Initialize: Initializes the method, attaching the Objects object as an event source (which can be understood here as the dispatch center bar) of the functions.
static function _removeeventlistener (Queue:object, event:string, handler): Void
An internal method that logs listeners who listen for event source-related events, and the RemoveEventListener method calls it.

The Eventdispatcher class has 4 public methods:

function Dispatchevent (eventobj:object): Void
/* Event Dispatch method, broadcast a message to the outside, with relevant description of the message (please compare with Broadcastmessage (msg:string) method)
EVENTOBJ: Event object, it should contain at least 2 attributes, assuming that the command is a dispatch center for an event, generally defined as follows:
var eventobj=new Object ();
eventobj.target= command;//The object that defines the broadcast message
Eventobj.type= "on offense";//define the specific type of message

Where the target attribute can also be undefined, the Dispatchevent method sets it as the object of the default broadcast message.

*/function Dispatchqueue (Queueobj:object, eventobj:object): Void
/* The Chinese meaning of this method, I call it "event communication" according to its function.
Queueobj: An event dispatch center object
EVENTOBJ: Event object, note: Unlike Dispatchevent, its target does not have a default value and requires you to manually set it.
Why is this method called an event communication? Because it allows the current event dispatch center to instruct another event dispatch center (the first parameter of the method) to broadcast the message to the outside world. In fact, the internal dispatchevent method called the Dispatchqueue, just the parameter queueobj set as the current event dispatch center.
*/function AddEventListener (event:string, handler): Void
/* Register a listener for an event
Event: Listener name for events
Handler: An object that listens for events
This method is more parameter event than the AddListener method of the Asbroadcaster class.
Identify the specific events that are being monitored.
*/
function RemoveEventListener (event:string, handler)
/* A supervisor who logs an event
Event: Events Name
Handler: The object of the listener event to unregister
This method, compared with the RemoveListener method of the Asbroadcaster class, has an extra parameter event, which explicitly logs out the listener object from which event.
*/

Here let us in the battlefield combat eventdispatcher it. Suppose there are two command centers, one is the headquarters, the other is the battlefield command. There are three units, namely artillery, infantry and death squads. Command can command artillery attack, Garrison and infantry, Field command can command infantry attack, charge and death squads, and command can communicate instructions to the field command. Here is the code that implements the complex relationship:
-----EVENTDISPATCHER TEST. FLA Start-----

Import Mx.events.EventDispatcher;
Import Mx.events.EventDispatcher Class
var command = new Object ();
command. Name = "command";
Eventdispatcher.initialize (Headquarters);
Attach the related functions of dispatch center to the Command object.
var battlefield command = new Object ();
Battlefield command. Name = "Field command";
Eventdispatcher.initialize (Battlefield command);
Attach the related functions of dispatch center to the battlefield Command object.
var infantry = new Object ();
Infantry. On offense = function (EVENTOBJ) {
Infantry's ' on offense ' event handling method, remember need to have parameters
var comes from =eventobj.target. Name;
Eventobj.target refers to an object that broadcasts the ' on offense ' event, with which you can access the event source and feed back the message.
var position =eventobj. position;
Gets the value of the ' position ' attribute on the event
Trace ("Infantry received" + from + "Incoming calls, light, speed to the enemy" + position + "forward!");
};
Infantry. On garrison = function (EVENTOBJ) {
var comes from =eventobj.target. Name;
Trace ("Infantry received" + from + "calls, on-site standby, more set sentry, to prevent the enemy!");
};
Infantry. On charge =function (EVENTOBJ) {
var comes from =eventobj.target. Name;
var position =eventobj. position;
Trace ("Infantry received" + from + "call, to enemy" + position + "launch charge!");
}
Headquarters. AddEventListener ("On Garrison", infantry);
Register the Listener ' infantry ' with the ' on-garrison ' incident at the headquarters
Battlefield command. AddEventListener ("On offense", infantry);
Registering for the ' on attack ' incident at the command ' infantry '
Battlefield command. AddEventListener ("On Charge", infantry);
Registering for the ' on charge ' incident at field Command ' infantry '
var artillery = new Object ();
Artillery. On offense = function (EVENTOBJ) {
var comes from =eventobj.target. Name;
var position =eventobj. position;
Trace ("Artillery received" + from + "calls, to the enemy" + position + "position violently bombardment!");
};
Artillery. On garrison = function (EVENTOBJ) {
var comes from =eventobj.target. Name;
Trace ("The artillery receives" + from + "calls, on-site standby, to ensure adequate ammunition preparation!");
};
Headquarters. AddEventListener ("On Garrison", artillery);
Headquarters. AddEventListener ("On offense", artillery);
Var Suicide Squad =new Object ();
Death squads. On charge =function (EVENTOBJ) {
var comes from =eventobj.target. Name;
var position =eventobj. position;
Trace ("Death squads received" + from + "calls, to the enemy" + Position + "launched the charge!" and desperate to win! ");
}
Battlefield command. AddEventListener ("On charge", death squads);
//---------------------------------------------------------------------
Trace ("command instructions:");
var eventObj1 = new Object ();
Defining Event Objects EventObj1
eventobj1.target = command;
To define the target attribute value for the event object EVENTOBJ1 is the command
Eventobj1.type = "on offense";
The event type that defines the event object is ' on offense '
EventObj1. Position = "505 position"
A custom attribute ' position ', you can attach more other attributes
Headquarters. Dispatchevent (EVENTOBJ1);
The command broadcasts the defined event object, and only the listener that registers the ' on offense ' attribute to it is received. The infantry did not attack.
Trace ("Field command indicates:");
var eventobj2=new Object ();
eventobj2.target= Battlefield Command;
This can be commented out because the Dispatchevent method automatically sets the target property of the event object to the object of the default broadcast message.
Eventobj2.type = "on offense";
EventObj2. Position = "105 position"
Field command. dispatchevent (EVENTOBJ2);
Trace ("Convey command instructions:");
var eventobj3=new Object ();
eventobj3.target= Battlefield Command;
The target attribute of the event object must be set here. Because Dispatchqueue does not set a default value for the target property
Eventobj3.type= "on charge";
EventObj3. Position = "123 position";
Headquarters. dispatchqueue (Field command, EVENTOBJ3);
During the war, perhaps the command did not know the layout of the frontline forces, so it could communicate the instructions to the field command, by the field command to broadcast the event object to its own related events registered listener
-----EVENTDISPATCHER TEST. FLA End-----

I'm not going to post the exact test results. Finally, talk about some interesting places in the Eventdispatcher class:
1. Objects that listen for events can be object, MovieClip, or function. For example:
Suppose that in the battlefield whenever the charge, you have to cheer, you can write:

function encourages morale () {
Trace ("Rush!!!!");
}
Field command. AddEventListener ("On charge", encouraging morale);

2, when the object of listening to the event is itself, the general wording is this:

Battlefield command. On charge =function () {
Trace ("Comrades, we will certainly win the final victory!") ");
}
Battlefield command. AddEventListener ("On Charge", field command);
But there is a simpler way to do it:
Battlefield command. On charge Handler=function () {
Trace ("Comrades, we will certainly win the final victory!") ");
}

This kind of writing no longer needs to use "AddEventListener" to register. It has two points to note, one is that it always executes before the event processing of other listener objects, and then it cannot log off the listener with the ' RemoveEventListener ' method.
3, the Listener object's event handler can also write this:

Infantry. Handlerevent=function (EVENTOBJ) {
var comes from =eventobj.target. Name;
var position =eventobj. position;
var E=eventobj.type;
Switch (e) {
Case "on offense":
Trace ("Infantry received" + from + "Incoming calls, light, speed to the enemy" + position + "forward!");
Break
Case "on Garrison":
Trace ("Infantry received" + from + "calls, on-site standby, more set sentry, to prevent the enemy!");
Break
Case "on charge":
Trace ("Infantry received" + from + "call, to enemy" + position + "launch charge!");
Break
Default
Break
}
Headquarters. AddEventListener ("On Garrison", infantry);
Battlefield command. AddEventListener ("On offense", infantry);
Battlefield command. AddEventListener ("On Charge", infantry);

OK, about Eventdispatcher class to this end, I hope you can grasp the Flash event mechanism and the flexibility to use them.



Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.