Cancelling MouseEvent.CLICK after MouseEvent.MOUSE_DOWN
I have a MOUSE_DOWN handler that cr开发者_开发技巧eates 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?
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);
}
stopping the propagation of an event only stops the event from flowing thru the display list, it doesn't reverse time and cancel or deregister the event.
ActionScript 3.0: The Event Flow
if you want to cancel an event just remove the event listener.
精彩评论