View comments | RSS feed

AsBroadcaster


Object
    |
    +-AsBroadcaster

public class AsBroadcaster
extends Object

Provides event notification and listener management capabilities that you can add to user-defined objects. This class is intended for advanced users who want to create custom event handling mechanisms. You can use this class to make any object an event broadcaster and to create one or more listener objects that receive notification anytime the broadcasting object calls the broadcastMessage() method.

There is no constructor function for the AsBroadcaster class. To use this class, follow this process:

Note: A common mistake is to capitalize the second letter of AsBroadcaster. When calling the AsBroadcaster.initialize() method, ensure that the second letter is lowercase. Any misspelling of AsBroadcaster fails silently.

Availability: ActionScript 1.0; Flash Player 6

Property summary

Modifiers

Property

Description

 

_listeners:Array [read-only]

A list of references to all registered listener objects.

Properties inherited from class Object

constructor, __proto__, prototype, __resolve


Method summary

Modifiers

Signature

Description

 

addListener(listenerObj:Object) : Boolean

Registers an object to receive event notification messages.

 

broadcastMessage(eventName:String) : Void

Sends an event message to each object in the list of listeners.

static

initialize(obj:Object) : Void

Adds event notification and listener management functionality to a given object.

 

removeListener(listenerObj:Object) : Boolean

Removes an object from the list of objects that receive event notification messages.

Methods inherited from class Object

addProperty, hasOwnProperty, isPropertyEnumerable, isPrototypeOf, registerClass, toString, unwatch, valueOf, watch



Version 8

Comments


mihnea_dev said on Sep 30, 2005 at 5:09 PM :
nice class to fast add event handling on custom classes.
I am glad that macromedia finaly added help support for this one.

If anyone wants a quick tip on how to add your custom events to your custom classes using the AsBroadcaster check this:
------------------------------------------------------
class SomeClass{
private var events:Object;
public function SomeClass(){
// ... whole constructor body not shown
events = new Object();
AsBroadcaster.initialize(events);
// ....
}
private function someFunction(){
// ... whole function body not shown
events.broadcastMessage("onSomething");
// ....
}
public function addListener(listener:Object)[
events.addListener(listener);
}
}
------------------------------------------------------
// use outside of class
var obj1:SomeClass=new SomeClass();
var listener1:Object = new Object();
var listener2:Object = new Object();
listener1.onSomething=function(){
// ... any actions here
}
listener2.onSomething=function(){
// ... any ohter actions here
}
obj1.addListener(listener1);
obj1.addListener(listener1);
------------------------------------------------------

That will create an instance of the SomeClass class and will assign to listeners to it wich will react differently on the same event.

Hope that helps,

Mihnea
Kanundrum said on Oct 21, 2005 at 11:30 PM :
I echo those sentiments. It's about time it been made "official". AsBroadcaster can greatly help in making you website/webapp more object oriented. I have been using this "hidden" feature for a while to do late binding and to facilitate communication between my objects. Note if you get tired of doing AsBroadcaster.initalize(object) you could do it on a prototype Example AsBroadcaster.initialize(Movieclip.prototype) (all movieclips will have the asbroadcaster goodness, well it worked in as1 i know some are still iffy about using prototypes).
Kanundrum said on Oct 23, 2005 at 11:47 AM :
oh btw broadcastMessage(event:String, param:String, param:string,..) if you want to pass the event/function some parameters
No screen name said on Jan 31, 2006 at 3:52 PM :
mihnea_dev, that code doesnt seem to work for me :(

and yea i did change that one brace
kavelle said on Jun 1, 2006 at 12:30 AM :
A better way to do it is to use the class itself as a broadcaster (saves the extra functions / variables). This is a very usefull feature!
------------------------------------------------------
class SomeClass{

//declare functions that are used (but they need not be defined)
var broadcastMessage:Function;
var addListener:Function;
public function SomeClass(){
AsBroadcaster.initialize(this);
}
private function someFunction(){
this.broadcastMessage("onSomething");
}
}
------------------------------------------------------

//and then outside the class
var myClassInstance:SomeClass = new SomeClass();
var myListener:Object = new Object();
myListener.onSomething = function(){
trace( "onSomething function event called" );
}
myClassInstance.addListener( myListener );
myClassInstance.someFunction();
UsmanH said on Jun 19, 2006 at 5:21 AM :
Please correct the syntex error on the first example given by mihnea_dev (sep 30, 2005)

Line 14: public function addListener(listener:Object)[

will be replaced with

public function addListener(listener:Object){

The coder has by mistake typed '[' brackets instead of '{' breakets
re:hush said on Mar 2, 2007 at 6:09 AM :
In order to fire kavelle's SomeClass's function "someFunction" outside the class make it public

 

RSS feed | Send me an e-mail when comments are added to this page | Comment Report

Current page: http://livedocs.adobe.com/flash/8/main/00001921.html