I added mouse up, down and click states. It kind of works. I can drag my object and move objects but my drags results in a final a click state( which is assigned to another method therefor a drag calls the method when the drag is finished).
How do I seperate a click and a drag that starts with a click and ends with a release?
I’m curious if this behaves any differently if you target the Flash target rather than another?
Instead of tracking MouseEvent.CLICK, perhaps you could track the mouse down and mouse up events only.
Here’s how I might implement it:
- Listen to MouseEvent.MOUSE_DOWN on the object
- In the listener, listen to MOUSE_MOVE on the stage, as well as MOUSE_UP on the stage
- In the move listener, update the drag position. Use a tween or exponential if you want it to have some “elastic” feel behind the mouse, and smooth out the movement
- In the up listener, remove the mouse move and mouse up listeners. If you would like to handle a click as well as a drag, save the time (Lib.getTimer) when you do the mouse down, as well as the original position. Then compare the new position in the up handler and the new time. If it has been fast enough and moved short enough, cancel any movement and handle as a click. Otherwise allow it to remain as a drag
Thanks for your reply. I did not think about using a timer. I have been using a mouse down vs current mouse down pos comparison (abs subtract) to see if the resulting numbers for x and y are smaller than some pixels. It works but I am wondering if there is even a cheaper way to to do that.
I will check out the timer idea.
As far as the mouse listener goes, I am already using the idea you mentioned. So that part works pretty well.
Yep, try combining both – that helps you know that they didn’t stray too far, and did it quickly. Otherwise a slow drag far away and back to the same position would be treated as a click