8 Replies Latest reply on Oct 30, 2009 5:46 PM by iceflex

    Need help with suctom event

    iceflex

      Hello Forum,

      Obviously I'm doing something wrong with the implementation of the custom events.

      I declared an event MyEvent class which extends flash.events.Event

       

      package com.myEvents
      {
           import flash.events.Event;

       

          public class MyEvent extends Event
           {
               public function MyEvent (type:String)
               {
                   super(type, true, true)
               }       
               public static const MY_EVENT_CHANGED:String = "myEventChanged";
              
               // Override the inherited clone() method.
               override public function clone():Event{
                   return new MyEvent (type);
               }   
           }
      }//end of calls

       

      I dispatched it from the non-component class, which extends EventDispatcher, in respond to some business notification.

      Some where in the application, I'm trying to intercept this event and set some text on myLabel:Label

      myLabel.addEventListener(MyEvent.MY_EVENT_CHANGED, onMyEvent)

      public function onMyEvent(myEvent:MyEvent):void{

          myLabel.text = 'Got the event'

      }

       

      The problem is that onMyEvent() function is never called.

      Can anyone tell me what am I doing wrong?

       

      Thanks a lot in advance,

      Mike

        • 1. Re: Need help with suctom event
          msakrejda Level 4

          Your custom event and your listener code look reasonable. How are you dispatching the event?

          • 2. Re: Need help with suctom event
            iceflex Level 1

            Thanks a lot for looking into it..

             

            var me:MyEvent = new MyEvent (MyEvent .MY_EVENT_CHANGED)
            var success:Boolean = dispatchEvent(me)

            //I debugged it: success == true

            • 3. Re: Need help with suctom event
              msakrejda Level 4

              So you're adding a listener to myLabel, but dispatching the event from another component--that's the problem. Unless you're taking advantage of display list capture/bubbling phase event dispatch, you need to add the listener to the EventDispatcher subclass that is dispatching the event.

              • 4. Re: Need help with suctom event
                iceflex Level 1

                Thanks a lot!

                Let me try....

                • 5. Re: Need help with suctom event
                  iceflex Level 1

                  Is that what you suggesting to do?

                  So, I'm adding a listener to the subclass of the EventDispatcher, where the event is dispatched from.

                  public class Subclass extends EventDispatcher{

                      public function Subclass(){

                          addEventListener(MyEvent.MY_EVENT_CHANGED, eventHandler)

                      }

                      public function eventHandler(event:MyEvent):void{

                           //handle the rendering of the label here ?

                   

                      }

                   

                      // dispatching event

                     //

                    //

                  }

                  • 6. Re: Need help with suctom event
                    JAcques TRIPOZ Level 1

                    Hi,

                     

                    Not *really* sure about this, but events can only follow a "Display List" branch.

                     

                    So if the object which throw the event is not in the same branch as the listener, the listener can get it. Or... if the 'thrower' is not a descender of the listener (added with addChild) it won't work. This seems to be your problem as it's dispatched from a non-component class extending EventDispatcher.

                     

                    You should add the listener to the "thrower" :

                     

                       var zeThrower:Thrower=new Thrower();
                       zeThrower.addEventListener("CustomEvent",fnHandler);

                     

                    HTH.

                    1 person found this helpful
                    • 7. Re: Need help with suctom event
                      msakrejda Level 4

                      Typically, you would add the handler in your application code, and not

                      in the Subclass constructor. It's more along of what JAcques is

                      saying--create the dispatcher in your application code, then add the

                      listener to the dispatcher.

                      1 person found this helpful
                      • 8. Re: Need help with suctom event
                        iceflex Level 1

                        Thanks to all...

                        Sorry, I was off for a while...I need to think as to how to work around this issue...I do not instantiate the dispatcher - "Subclass" - it's a Cairngorm commander, which is instantiated by the framework...

                        Thanks a lot for you input and explanations, at least now I understand the root cause of the issue and that's a lot.

                         

                        Thanks,

                        Mike