The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

# Thread: calculate the mouse movement distance

1. 22
posts
Registered User

## calculate the mouse movement distance

I am wondering if anyone can share with a good example on how to detect the distance of the mouse movement in a certain time. I’d like to be able to calculate the distance the mouse was moved from one point to another in certain time (one second) and compare these values. Please let me know if you have any good sample. Thank you in advance.

2. As far as I know, there's no built-in API to do this. You might just have to store the mouse X/Y position at a given time, then later compare it to the current mouse position.

If you want to figure out how much it moved as opposed to its displacement, that would be trickier. That is, in one second, the user could have moved the mouse across the entire screen then back to the starting point. If you compared to start and end points, it would say the user moved very little (or none at all). If that's an issue, then you'll probably have to add up all the distances traveled each frame instead of simply comparing the start and end points.

Hope that helps!

3. 39
posts
Registered User
You are going to have to use some flash math. More specifically the Pythagorean theory to get the distance. A good tutorial dealing with proximity is http://www.gotoandlearn.com/play?id=17.

4. 68
posts
Registered User
Code:
```var mX:Number = _xmouse;
var mY:Number = _ymouse;

function checkDistance()
{
clear();
//trace('new distance: ' + Math.sqrt(Math.pow((mY - _ymouse), 2) + Math.pow((mX - _xmouse), 2)));
lineStyle(1, 0x000000);
moveTo(mX, mY);
lineTo(_xmouse, _ymouse);
mX = _xmouse;
mY = _ymouse;
}

setInterval(checkDistance, 1000);```
sample of post 2...

There are currently 1 users browsing this thread. (0 members and 1 guests)