Search code examples
actionscript-3eventsstoppropagation

Cancelling MouseEvent.CLICK after MouseEvent.MOUSE_DOWN


I have a MOUSE_DOWN handler that creates a CLICK event listener on a child object. Naturally, as soon as you release the mouse button, if you happen to be over the child object, the CLICK event fires.

I was disappointed to discover that event.stopImmediatePropagation doesn't interrupt the CLICK event from registering the MOUSE_DOWN as part of its detection cycle. It makes sense that it doesn't but still... disappointing.

A MouseEvent.CLICK consists of detecting a MOUSE_DOWN on the object and then if it's followed by a MOUSE_UP without leaving the object, he event fires. I had hoped that by cancelling the MOUSE_DOWN event, it would clear that out of the CLICK buffer, but no such potatoes, alas.

Any tricks out there? This could all be handled with a flag and a couple more MOUSE_UP and MOUSE_DOWN handlers, but dang, smacks of effort... Buehler?


Solution

  • If I understand you correctly, you should be able to correct this behavior in the following way:

    Instead of a MOUSE_DOWN handler, have a CLICK handler.

    That CLICK handler will remove the listener associated with it, and create a new CLICK listener with a reference to the function you want to be the new CLICK handler.

    IE:

    //on CreationComplete, or wherever you are adding the MOUSE_DOWN listener
    addEventListener(MouseEvent.CLICK, handler1);
    
    private function handler1(aEvent:MouseEvent):void
    {
        removeEventListener(MouseEvent.CLICK, handler1);
        addEventListener(MouseEvent.CLICK, handler2);
    }