This example describes how JavaScript detects the direction of mouse movement. Share to everyone for your reference. The implementation methods are as follows:
function Start () {
Stage.addeventlistener (mouseevent.mouse_move, checkdirection);
}
Start ();
var prevx=0;
var prevy=0;
var curx=0;
var cury=0;
var dirx:string= "";
var diry:string= "";
function Checkdirection (e:mouseevent) {
trace ("X movement:" + gethorizontaldirection () + ", Y movement:" + Getverti Caldirection ());
E.updateafterevent ();
}
function Gethorizontaldirection (): String {
Prevx=curx;
Curx=stage.mousex;
if (Prevx>curx) {
dirx= ' left ';
} else if (PrevX < CurX) {
dirx= ' right ';
} else {
dirx= ' None ";
}
return DirX;
}
function Getverticaldirection (): String {
Prevy=cury;
Cury=stage.mousey;
if (Prevy>cury) {
diry= "up"
} else if (Prevy < Cury) {
diry= ' down ';
} else {
diry= ' none ' ;
}
return diry;
}
I hope this article will help you with your JavaScript programming.