7 Replies Latest reply on Jun 22, 2006 1:13 AM by Peter Lorent

    Adding Event Broadcasting to a Class

    SPGAnne Level 1
      A newbie here, so please be gentle.

      I am trying to create a new class where I want each instance to be able to broadcast messages. I have tried to use AsBroadcaster. In the constructor function I have

      AsBroadcaster.initialize(this);

      Then inside one of my class's private methods

      this.broadcastMessage("myEvent");

      When I check the script, I get an error saying "There is no method with the name 'broadcastMessage' ".

      Is it not possible to use AsBroadcaster inside a class? If not, what would be a good/easy way to add this kind of functionality?

      In one of my planned uses of this I could have 100's of instances that need to broadcast, so I need a light weight easy way to do this.

      Thanks in advance for anyone who can help me.
        • 1. Re: Adding Event Broadcasting to a Class
          Peter Lorent Level 2
          >>Is it not possible to use AsBroadcaster inside a class?
          Sure you can. You just need to add the following properties to the class definition:
          private var broadcastMessage:Function;
          public var addListener:Function;
          • 2. Re: Adding Event Broadcasting to a Class
            SPGAnne Level 1
            LuigiL comes to my rescue and aid again. Thank you so very much!

            I did as you suggested and also found some more sample code in the LiveDocs section on AsBroadcaster. All works if I call a method from outside the class, but I run into a snag when I try to do this from inside the magic stuff you helped me with in my other post about handling onRollOver stuff inside a custom class. Specifically, this is the code that doesn't seem to work:
            class Hotspot {
            ...
            private var hotspot_mc:MovieClip; // reference to a movie clip that will provide up, over, down appearance
            private var cursor:CustomCursor; // this is a reference to the customCursor
            private var broadcastMessage:Function;
            public var addListener:Function;

            // Constructor
            public function Hotspot(appearanceMC:String, // this is a movie clip which has up, over, down states
            w:Number, h:Number,
            x:Number, y:Number,
            target:MovieClip, depth:Number) {
            hotspot_mc = target.attachMovie(appearanceMC,"hotspot_mc"+depth,depth);
            // plus some other function calls for setting the size and position of the movieclip
            setRollOver(hotspot_mc);
            AsBroadcaster.initialize(this);
            }
            private function setRollOver(target:MovieClip):Void{
            // target is this hotspot instance's movie clip
            var thisObj:Hotspot=this;
            target.onRollOver=function():Void{
            target.gotoAndPlay("over");
            thisObj.broadcastMessage("onHotspotOver"); //this doesn't work, event never gets to the listener
            }
            }

            -------------
            outside the class on the main timeline I have:
            var h:Hotspot = new Hotspot("M-Hotspot Appearance",20,20,215,226,this,this.getNextHighestDepth());
            var hListener:Object = new Object();
            hListener.onHotspotOver = function(){
            trace( "onHotspotOver event received" );
            }

            I don't seem to get the event at the listener object. I tried a bunch of experiments where I try to redirect inside the class to another function (i.e. I put: thisObj.requestBroadcast("onHotspotOver") in the setRollOver stuff, then wrote another private function called requestBroadcast(event:String) which does: this.broadcastMessage(event). The call gets from the setRollOVer stuff to the requestBroadcast function OK, but the broadcast doesn't seem to get out of the class to the listener. I also tried calling the requestBroadcast from outside the class by writing a public method called testBroadcast which in turn calls the private function of requestBroadcast . I then put this code on the main timeline: h.testBroadcast("onHotspotOver");
            In that case I *do* get the onHotspotOver event delivered to my listener.

            So, again, I'm stuck. Your continued patient help is very much appreciated.


            • 3. Adding Event Broadcasting to a Class
              Peter Lorent Level 2
              You forgot to add the listener...

              var h:Hotspot = new Hotspot("M-Hotspot Appearance",20,20,215,226,this,this.getNextHighestDepth());
              var hListener:Object = new Object();
              hListener.onHotspotOver = function(){
              trace( "onHotspotOver event received" );
              h.addListener(hListener);
              }
              • 4. Re: Adding Event Broadcasting to a Class
                SPGAnne Level 1
                Actually, I had added the listener, but did something else equally boneheaded. I had actually instantiated 2 hotspots but had not added the listener code for the second one, and that's the one I kept moving my mouse over. Oh boy. Duh. As you mentioned in the other topic you helped me with...god is in the details.

                However, this mistake brings to mind the next dilemna I face which is, I could have on the order of 100+ of these hotspots in my application so how to best design for listeners and functions triggered by the broadcasted message from each hotspot. In my current toolset (not Flash) what I do is instantiate all the hotspots. I have an "agent" object which knows how to deal with onHotspotOver stuff depending on where/how these hotspots are used. With my current tool I don't have to register each of the hotspots, and each instance sends the same custom message "onHotspotOver" always to the "agent" object. So how would I do that in Flash? It looks like each hotspot instance has to register itself (I guess that's OK, I can just do that in a loop as I instantiate them. - I assume I can use a string concatenation to do that e.g. "h" + numberString.addListener("h" + numberString + "Listener"); where numberString is an index 1, 2,...n I set in my loop) But how can I create the equivalent of my "agent " rather than having to define a function for each listener? i.e. how can I get all instances of the hotspots to send their message to just one place, the "agent"?

                Again, I am very much obiliged for your willingness to help me and your patience with all my bumbling about.
                • 5. Re: Adding Event Broadcasting to a Class
                  Peter Lorent Level 2
                  The agent will be a wrapper class that receives all notifications.
                  But, to give a good advice, I would need a more detailed outline of the app. You have to know, I'm not a native speaker and sometimes it's hard to fully comprehend the question. If you want to keep the details offline I can send you my email address.
                  • 6. Re: Adding Event Broadcasting to a Class
                    SPGAnne Level 1
                    Wow, how generous of you. I would be most grateful to get your email address. How do I give you mine without having to post it here? (Just trying to avoid excess spam).

                    P.S. Your English is excellent. I never would've known it was a second language for you.
                    • 7. Re: Adding Event Broadcasting to a Class
                      Peter Lorent Level 2
                      You can send your message to luigil at mac dot com