Search code examples
actionscript-3eventsdispatchevent

Multiple dispatchHandler Actionscript 3


I want to dispatch an Event 2 times:

I have a MainClass, a SecondClass and a ThirdClass.

In the ThirdClass, there's a clickEvent. I dispatch it to the SecondClass:

    this.addEventListener(MouseEvent.CLICK, clickHandler);


    public static const CLICKED_HANDLER:String = "clickedHandler";

    public function clickHandler(e:MouseEvent):void {
        dispatchEvent(new Event(CLICKED_HANDLER));
    }

I catch and throw it in the SecondClass:

    object.addEventListener(ThirdClass.CLICKED_Handler, clickedEventListener);


    public static const CLICKED_HANDLER:String = "clickedHandler";

    public function clickedEventListener(e:Event):void {
        dispatchEvent(new Event(CLICKED_HANDLER));
    }

And this I catch in the MainClass:

    object.addEventListener(SecondClass.CLICKED_HANDLER, clickedEventListener);

    public function clickedEventListener(e:Event):void {
        trace("click");
    }

But it wouldn't work... What am I doing wrong? And how could I get information about the object of the ThirdClass that's clicked? (Normaly with 1 dispatchEvent, it's with:

    var thirdClassObject:ThirdClass = e.currentTarget as ThirdClass;

in the clickHandler method, but how to do this with 2 dispatchEvents?)

So: I want to know in my MainClass which ThirdClass-object is clicked.

Thanks a lot!


Solution

  • When you dispatch an event, the target property references the dispatcher.
    What you apparently need is passing a reference to third class along with the event to use in the event handler of main class.

    You have several options to achieve this.

    1. If a dispatcher instance is on the display list, use event bubbling to handle events in parent display objects. Refer to the second argument of Event constructor and Event.bubbles property. This way you can subscribe to any events of child objects in a parent, and check Event.target property in your event handler. Most of mouse events like MouseEvent.CLICK are bubbling by default. So you could just listen to them and check targets in main class.

      // inside MainClass
      // notice: we are subscribing to MainClass instance
      this.addEventListener(MouseEvent.CLICK, clickedEventListener);
      public function clickedEventListener(e:MouseEvent):void { trace(e.target); // the target is what was actually clicked // you may also notice the difference between e.target and e.currentTarget }
      If you still want to use CLICKED_HANDLER event, you may do so as follows:
      // inside ThirdClass
      dispatchEvent(new Event(CLICKED_HANDLER, true));
      // ---------------------------------------^
      Remember: this will only work for display list members.
      See the Event flow article on adobe.com.

    2. Create a custom event class and place reference there. (And you can also use event bubbling with custom event classes.) This way you will dispatch your event as

      // inside SecondClass
      public function clickedEventListener(e:Event):void {
          var customEvent:CustomEventClass = new CustomEventClass(CLICKED_HANDLER);
          customEvent.customTarget = e.target;
          dispatchEvent(customEvent);
      }
      // inside MainClass public function clickedEventListener(e:CustomEventClass):void { trace(e.customTarget ); }