1 Reply Latest reply on Sep 9, 2007 7:32 PM by crazygringo

    Why isn't EventDispatcher working?

    crazygringo
      Hi,

      I'm trying to get EventDispatcher to work, and it simply won't. I was hoping somebody might be able to spot the error, I've been at this for 2 hours, using http://www.adobe.com/devnet/flash/articles/creating_events.html as a model.

      I have a MovieClip symbol called "WindyButton" that contains a Button instance called "btnOutline" and a text field on top of it. The WindyButton symbol is associated with a class, which contains the code:

      import mx.events.EventDispatcher
      class WindyButton extends MovieClip
      {
      private var btnOutline:Button;

      function dispatchEvent() {};
      function addEventListener() {};
      function removeEventListener() {};

      public function WindyButton()
      {
      btnOutline.useHandCursor = false;
      btnOutline.onRelease = this.c;
      mx.events.EventDispatcher.initialize(this);
      }
      public function c()
      {
      trace ("sending event...");
      var eventObject:Object = {target:this, type:'click'};
      dispatchEvent(eventObject);
      }
      }

      Then, I've put an instance of the WindyButton symbol onto my stage, with an instance name of myButton. The first frame of the timeline contains:

      var listener:Object = new Object;
      listener.click = function(evtObj) {
      trace ("event received");
      }
      myButton.addEventListener("click", listener);

      From all the tutorials I've read it seems I'm doing things correctly. But then when I click the button, my trace output only says "sending event..." and never says "event received".

      Can anybody see what I'm doing wrong???

      I'm using Flash 8 and ActionScript 2.0.

      Much thanks
      Michael
        • 1. Re: Why isn't EventDispatcher working?
          crazygringo Level 1
          Never mind, after another hour I finally figured out that the problem is one of event scope.

          If anybody's curious, the answer was to write:

          public function WindyButton()
          {
          mx.events.EventDispatcher.initialize(this);
          btnOutline.useHandCursor = false;
          var thisObj:WindyButton = this;
          btnOutline.onRelease = function()
          {
          trace ("sending event...");
          var eventObject:Object = {target:thisObj, type:"click"};
          thisObj.dispatchEvent(eventObject);
          };
          }