i have two buttons, each half the width of the screen and both the entire height of the stage. So one button would be on the left of screen and the other, the right. I have eventlisteners for both when the buttons are pressed and released.
My problem is that if i click one button and drag to the other side, the event take it that the mouse is not released.
For example the right button should make my character fly, so when i click on it, it flies and if i drag my mouse to the left button and release, my character continues flying. I believe this is due to the right button being unable to detect the mouse up when its off the button shape. Is there any way around it?
1
votes
2 Answers
0
votes
Consider
MouseEvent.MOUSE_OUT as well.
When you start dragging (MOUSE_DOWN) set a isCurrentlyDragging boolean to true (defined outside of the event handler function of course). On MOUSE_OUT if it is currently dragging then decide wether to handle it as a MOUSE_UP.
On MOUSE_UP and on MOUSE_OUT set isCurrentlyDragging = false;
0
votes
This was possible to detect easily in AS2, but they removed it (for some reason) in AS3.
What I do to get around this is add a global mouseUp-listener;
stage.addEventListener(MouseEvent.MOUSE_UP, mouseUpHandler);
function mouseUpHandler(e:Event) {
if( _clickedObject ) _clickedObject.dispatchEvent(new Event("releasedOutsideEvent"));
}
And then in my MOUSE_DOWN handlers i add
_clickedObject = e.target;
and in the MOUSE_UP handler
_clickeObject = null;
Or something like that. There might be a bug or two there, but it should work.