2 Replies Latest reply on Jan 16, 2008 9:08 AM by ctaffe

    Simple Custom Event Help

    ctaffe
      I'm trying to get a better grasp of Events and creating my own custom events and dispatchevent calls. This is in reference to a previous post here: http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=60&catid=585&threadid =1329712&enterthread=y

      I initially tried to get this working in the midst of a bunch of other stuff going on but could not get my disptachevent to trigger the listener I had set up. After messing around for awhile I decided to just pull this part out and try to get the basics of it working (which I'm still having problems with). While I can find plenty of examples making listeners work off of predefined events (like mouse clicks) I can't find much on what I'm trying to do. I'm sure I'm missing a very simple property of Events and doing a call wrong in here but I can't figure out what that is.

      quote:


      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="createListener()">

      <mx:Script>
      <![CDATA[

      import mx.controls.Alert;

      public function createListener():void{
      var dataListener: EventDispatcher = new EventDispatcher();

      dataListener.addEventListener("myEvent", retrieverListener);
      Alert.show("listener created");
      }

      public function retrieverListener(event:Event):void{
      Alert.show("event triggered");
      }

      private function tempFunction():void{
      dispatchEvent(new Event("myEvent"));
      }

      ]]>
      </mx:Script>

      <mx:Button x="108" y="193" label="Button" click="tempFunction()"/>

      </mx:Application>



      The alert fires off on load that the createListener function is called but when I push the button, the tempFunction is called but the dispatch never fires off my retriever function. While the above program could easily function off of the mouseclick event on the button, my final product wont be firing off of that. I'm just using it here as a simple example.

      Thanks!
        • 1. Re: Simple Custom Event Help
          m_hartnett Level 3
          The problem is with the following lines:
          var dataListener: EventDispatcher = new EventDispatcher();
          dataListener.addEventListener("myEvent", retrieverListener);

          The dataListener should not be listening for the event ( it could in other scenarios) but in your case you want to change it to this.addEventListener or just addEventListener (implied this).

          An eventDispatcher is used in the event that the object does not have a dispatcher such as in some ActionScript component.

          Since you are within your application you can use the application eventDispatcher.

          Here is your code with the slight modifications:

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="createListener()">

          <mx:Script>
          <![CDATA[

          import mx.controls.Alert;

          public function createListener():void{
          addEventListener("myEvent", retrieverListener);
          }

          public function retrieverListener(event:Event):void{
          Alert.show("event triggered");
          }

          private function tempFunction():void{
          dispatchEvent(new Event("myEvent"));
          }

          ]]>
          </mx:Script>

          <mx:Button id="btn" x="108" y="193" label="Button" click="tempFunction()"/>

          </mx:Application>


          • 2. Re: Simple Custom Event Help
            ctaffe Level 1
            Thank you!

            I knew it was something simple that I was delcaring wrong.