|
-
The built in event system does not include a "broadcast" functionality. But you can pretty easily build one yourself by making a single instance to dispatch your events.
Code:
public class Broadcaster extends EventDispatcher {
private var _instance:Broadcaster = null;
//DO NOT USE THIS. Use static methods broadcastEvent and addBroadcastListener instead
public function Broadcaster(){
if (_instance != null){
throw new IllegalOperationError("Broadcaster constructor called more than once");
}
}
private static getInstance():Broadcaster{
if (null == _instance){
_instance = new Broadcaster();
}
return _instance;
}
public static broadcastEvent(event:Event):void{
getInstance().dispatchEvent(event);
}
public static addBroadcastListener(type:String, listener:Function, useCapture:Boolean = false, priority:int = 0, useWeakReference:Boolean = false):void{
getInstance().addEventListener(type, listener, useCapture, priority, useWeakReference);
}
}
Then you can register a listener from anywhere:
Code:
Broadcaster.addBroadcastListener("someEvent", somehandler);
And trigger dispatch from anywhere
Code:
Broadcaster.broadcastEvent(new Event("someEvent"));
-
 Originally Posted by jweeks123
Code:
dispatchEvent(new Event("DispatcherClassEvent", true));
addEventListener("DispatcherClassEvent", doThisFunction);
function doThisFunction(evt:Event):void {
trace("Woo Hoo");
}
Just changing "bubbles" to true doesn't seem to work. At least not with objects outside of the dispatcher's display list hierarchy. Using the above I could "hear" the event in the dispatcher's parents... but not in the dispatcher's 3rd cousins twice removed.
 Originally Posted by 5TonsOfFlax
The built in event system does not include a "broadcast" functionality. But you can pretty easily build one yourself by making a single instance to dispatch your events.
Thanks, I've used classes like that with static functions before, don't know why it didn't occur to me to make a Broadcaster like that. Mainly I've used them to store utility functions that I use frequently, like truncating text, or generating random numbers, etc. The chief benefit, for me, was that instead of something like:
Actionscript Code:
var myUtilFunctions:UtilFunctions = new UtilFunctions(); myUtilFunctions.someFunction();
I could just:
Actionscript Code:
UtilFunctions.someFunction();
In fact, you essentially tell me the same by noting "//DO NOT USE THIS." in regards to the constructor.
but, there are a couple of nuances in your code I don't quite understand.
1) I'm used to seeing the word "function" after "static", is that a typo? Or are getInstance(), broadcastEvent() and addBroadcastListener() not what I know as methods?
2) What is the benefit/purpose to the getInstance() method? The Broadcaster class could be used "instance-less" couldn't it? Why use one of these over the other:
Actionscript Code:
public static broadcastEvent(event:Event):void{ getInstance().dispatchEvent(event); }
public static broadcastEvent(event:Event):void{ dispatchEvent(event); }
ps. Those utility classes I had created before never extended anything... since they really had no higher functionality than as a storage area for functions. So does extending an actual class (i.e. EventDispatcher) needs to be handled differently?
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|