dispatching event

5 posts

Flag Post

So here is the problem:
I have a class called classA. here are a few EventListeners like MouseEvent.MOUSE_UP and MOUSE_DOWN. in this class happens some checks (like, is the object disabled etc). I dispatch this event, since I would like to use this in the document class. but only when the checks return true

protected function MouseUpHandler(me:MouseEvent) {
    if (checks){
        dispatchEvent(me);
    }
}

another classB extends classA. here these EventListerners are overridden to add extra functionality

override protected function MouseUpHandler(me:MouseEvent) {
    super.MouseUpHandler(me);
    AddExtraFunctionalities();
}

in the main class, I make a new classB. and classB.addEventListener(MouseEvent.MOUSE_UP, MouseUpHandler);
however, when the MOUSE_UP happens, classA and classB become in a unending loop.

Error #2094: Event dispatch recursion overflow.

how should I do it right? without using custom events.

great thanks

 
Flag Post
protected function MouseUpHandler(me:MouseEvent) {
    if (checks){
        return true;
    }
    else {
        return false;
    }
}
override protected function MouseUpHandler(me:MouseEvent) {
    if(super.MouseUpHandler(me)) {
        AddExtraFunctionalities();
    }
}

What you actually did was to cause a mouse event to trigger a function that then passed that original event back to itself. Anytime classB dispatches the MOUSE_UP event, the handler for that function dispatches a MOUSE_UP event from that same object. That’s what you told it to do.

 
Flag Post

That looks great, but the main document class still caches every MouseEvent.MOUSE_UP. Is there a way the main class only catches the event when the checks == true

 
Flag Post

Instead of:

protected function MouseUpHandler(me:MouseEvent) {
    if (checks){
        dispatchEvent(me);
    }
}

Do this:

protected function MouseUpHandler(me:MouseEvent) {
    if (!checks){
        me.stopImmediatePropagation();
        me.preventDefault();
    }
}
 
Flag Post

That did the trick. many many thanks!