Detecting Direction of Mouse Movement in AS 3

September 20th, 2008     |     9 Comments

In ActionScript 2, you had the watch function that, among many things, allowed you to detect which direction your mouse cursor was moving.There is a tutorial on how to do that on kirupa.com itself. In ActionScript 3, I couldn’t find the watch function nor an equivalent replacement. My few seconds of searching the web didn’t help much except to, ironically, refer to the older tutorial that I had forgotten about over the years.

Creating your own function that shows you how to detect the direction of mouse movement is not very complicated, though. The idea is very simple:

Given where your mouse is currently, where was it earlier?

The answer to that question gives you everything you would need to determine in what direction your mouse is currently moving in! The answer in code-form is below:

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: " + GetVerticalDirection());
 
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;
}

Just copy all of the above code into a blank keyframe on your timeline, and run the movie. As you move your mouse, the Output window will give you immediate feedback on what direction your mouse is currently moving in:

image

I am not going to describe the code in any great detail here, for it is very straightforward. The prev variables store the position of your mouse from an earlier call of the GetVerticalDirection or GetHorizontalDirection function. The cur variables get the current mouse position. Comparing the current values with the previous values gives you the information you would need to determine which direction your mouse is currently moving in.

You may be wondering how I am getting the values from the previous function call? The answer is simple – look at the ordering between the prevX/prevY variables and the curX/curY variables. At each mouse move, your cur variables get updated to the current position. This update, though, is preceded by you setting the prev variables to the earlier value stored in curX and curY.

This ensures that your prevX and prevY variables are ALWAYS one step behind your curX and curY variables. For what we are trying to do, being one step behind is good!

Cheers!
Kirupa :)

9 Responses to “Detecting Direction of Mouse Movement in AS 3”

  1. mark mun Says:

    Nice. simple yet elegant.

  2. Pasquale Says:

    Oh man, even after being a member of your forums for so long, I know nothing about ActionScript.

    You still love me right?

  3. kirupa Says:

    It’s never too late to learn :P

  4. Detecting mouse movement directions « :maohao: Says:

    [...] Detecting mouse movement directions Here [...]

  5. Biju Subhash Says:

    Nice Tutorials… :)
    Thank You Kirupa

  6. fred Says:

    a good article.

  7. Weezeroz Says:

    Познавательный и интересный материал, думаю стоит обсудить это всем вместе. Кто хочет присоединиться?

  8. Окулист Says:

    Как раз то что искал, большое спасибо!

  9. allianceblaa Says:

    вот ведь лажа

Leave a Reply