Detecting Direction of Mouse Movement

Hey everyone,
I came up with two simple functions for determining the direction the mouse is moving, and I’m wondering if you all have an easier way of doing this:

vertical_check = function () {
 j = 0;
 v = [];
 this.createEmptyMovieClip("tempy", this.getNextHighestDepth());
 tempy.onMouseMove = function() {
  j++;
  v[j] = this._ymouse;
  if (j>1) {
   if (v[j]>v[j-1]) {
    trace("moving down");
    j = 0;
    v = [];
   } else {
    trace("moving up");
    j = 0;
    v = [];
   }
  }
 };
};
horizontal_check = function () {
 i = 0;
 h = [];
 this.createEmptyMovieClip("tempx", this.getNextHighestDepth());
 tempx.onMouseMove = function() {
  i++;
  h* = this._xmouse;
  if (i>1) {
   if (h*>h[i-1]) {
    trace("moving right");
    i = 0;
    h = [];
   } else {
    trace("moving left");
    i = 0;
    h = [];
   }
  }
 };
};
horizontal_check();
vertical_check();

This isn’t necessarily a bug fix, for my above code works. It just seems too complicated for something so simple. I’m planning on making this into a quick tutorial, so be warned that I may use your suggestions (with credit) in my writeup :slight_smile:

Thanks!
Kirupa :beam:

anyone know how to more accuratly determine mouse movement

like moving directly verticle would be 0
moving dead right would be 90
moving directly toward bottom would be 180
moving dead left would be 270

or some way to get the exact trajectory?

Try messing with the trigonometry. Most of these methods form a right triangle between the old and new positions, and then use a trigonometric function to find the angle. :slight_smile:

heheh…ummm yeh i took a trig class in H.S. like 10 years ago…

Are you guys trying to just get something to point at the mouse???
If so…it’s really easy, use trig! Here, I’ll make it easy for you.
Now look at this:

var distx:Number = _root._xmouse-_x;//finds the x distance between the mouse and your movie clip
var disty:Number = _root._ymouse-_y;//finds the y distance between the mouse and your movie clip
var rad:Number = Math.atan2(disty,distx);//finds radians
_rotation = rad/(Math.PI/180);//Makes your movie clip rotate towards its target, the mouse

Hope I helped!

Then you should be good at it. :stuck_out_tongue:

[whisper]Just play with the functions until it works…[/whisper]

Hello there Is there way th check left and right movement in a specific region?

Thanks