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!
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.
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
If you still want to useEvent
constructor and Event.bubbles property. This way you can subscribe to any events of child objects in a parent, and checkEvent.target
property in your event handler. Most of mouse events likeMouseEvent.CLICK
are bubbling by default. So you could just listen to them and check targets in main class.CLICKED_HANDLER
event, you may do so as follows:Remember: this will only work for display list members.
See the Event flow article on adobe.com.
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