5 Replies Latest reply on Jul 7, 2006 12:58 AM by Popeye the Sailorman

    broadcast events between classes (objects)

    henk_jan
      Hello,
      I'm can't figure out how to broadcast an event to another class.
      In actionscript 2 I created 2 classes. The first class called the second class to load a file from an url. When the file was loaded I used dispatchEvent({type: "fileLoaded", target: this, filename: sFilename})
      In the first I simply wrote addEventListener("fileLoaded", handleFile)
      But with AS3 this no longer works.

      Can anyone tell me how to accomplish this? Because I can't find a clear example and I don't know how to do this.

        • 1. Re: broadcast events between classes (objects)
          Popeye the Sailorman
          I use the following code to catch a mouseevent in a class and dispatch another event in actionscript3:

          protected function update(mouseEvent:MouseEvent):void {
          var event:UpdateEvent = new UpdateEvent(UpdateEvent.UPDATE);
          dispatchEvent(event);
          }

          The UpdateEvent is a separate class in a package calle Components:

          // ActionScript file
          package Components {
          import flash.events.Event;

          public class UpdateEvent extends Event
          {
          public var isEnabled:Boolean;
          // the following variables are added by me
          public static const UPDATE:String = 'update';
          public var position:Number = 0;
          public var index:int = 0;
          public var status:Boolean;
          public function UpdateEvent(type:String, isEnabled:Boolean=false) {
          super(type);
          this.isEnabled = isEnabled;
          }
          public override function clone():Event {
          return new UpdateEvent(type, isEnabled);
          }
          }
          }

          In the class that listens to the events just add the following:

          instance.addEventListener(UpdateEvent.UPDATE, function);
          instance is an instance of the class where the dispatch takes place

          hope this helps
          • 2. Re: broadcast events between classes (objects)
            henk_jan Level 1
            Thanks for answering.
            If I implement what you wrote I get the following Errors:
            - In the class that dispatches the event (XMLData) I get 1180: Call to a possibly undefined method dispatchEvent
            - And in the class with the addEventListener (Data.as) I get 1061: Call to a possibly undefined method addEventListener through a reference with static type classes.dbdata:XMLData

            My custom event (in a diverent package looks like this:
            package classes.utils {
            import flash.events.Event;

            public class PreloadEvent extends Event {
            private var _oData:Object;
            public static const LOADED:String = 'loaded';
            public function PreloadEvent(type:String, data:Object=null) {
            super(type);
            _oData = data;
            }

            public function get Data():Object {
            return _oData;
            }

            public override function clone():Event {
            return new PreloadEvent(type, _oData);
            }
            }
            }
            • 3. Re: broadcast events between classes (objects)
              Popeye the Sailorman Level 1
              In the defintion of your custom event I miss the isEnabled part (don't know if that has anything to do with the issue though). Below a custom event that works for me

              package Components {
              import flash.events.Event;

              public class ZoomEvent extends Event
              {
              public var isEnabled:Boolean;
              public static const ZOOM:String = 'zoom';
              public var vertical:Boolean = true;
              public var minvalue:Number = 0;
              public var maxvalue:Number = 0;
              public var zoomfactor:Number = 0;
              public function ZoomEvent(type:String, isEnabled:Boolean=false) {
              super(type);
              this.isEnabled = isEnabled;
              }
              public override function clone():Event {
              return new ZoomEvent(type, isEnabled);
              }
              }
              }

              Furthermore I have somewhere read in the documentation that using custom event should go together with adding an embedded statement like: [Event(name="update", type="Components.UpdateEvent")] in the class where you use the event, but for me it also works without this embedding code (???)

              And is your class XMLData a subclass from a class that supports dispatchEvent. I do not know what classes support dispatchEvent, maybe all. Then this remark makes no sense.

              Good Lcuk
              • 4. Re: broadcast events between classes (objects)
                henk_jan Level 1
                I made it work!
                The trick was to extend the XMLData classe with EventDispatcher (class XMLData extends EventDispatcher)
                This way the XMLData class 'understands' dispatchEvent.

                So in short: to use dispatchEvent in a class you have to extend is with EventDispatcher.

                • 5. Re: broadcast events between classes (objects)
                  Popeye the Sailorman Level 1
                  Ok That's nice.

                  Them my remark "And is your class XMLData a subclass from a class that supports dispatchEvent. I do not know what classes support dispatchEvent, maybe all. Then this remark makes no sense" makes sense after all.

                  Have fun