A Flash Developer Resource Site

Results 1 to 12 of 12

Thread: [RESOLVED] Dispatch and Listen for a Global Event

Hybrid View

  1. #1
    Will moderate for beer
    Join Date
    Apr 2007
    Location
    Austin, TX
    Posts
    6,801
    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"));

  2. #2
    Senior Member
    Join Date
    Jan 2006
    Posts
    133
    Quote Originally Posted by jweeks123 View Post
    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.


    Quote Originally Posted by 5TonsOfFlax View Post
    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
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center