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:
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





September 21st, 2008 at 4:51 pm
Nice. simple yet elegant.
September 22nd, 2008 at 12:45 am
Oh man, even after being a member of your forums for so long, I know nothing about ActionScript.
You still love me right?
September 22nd, 2008 at 5:39 pm
It’s never too late to learn
September 30th, 2008 at 10:52 am
[...] Detecting mouse movement directions Here [...]
November 20th, 2008 at 1:04 am
Nice Tutorials…
Thank You Kirupa
December 23rd, 2008 at 11:58 pm
a good article.
October 1st, 2009 at 12:17 pm
Познавательный и интересный материал, думаю стоит обсудить это всем вместе. Кто хочет присоединиться?
October 5th, 2009 at 2:28 am
Как раз то что искал, большое спасибо!
October 15th, 2009 at 4:40 am
вот ведь лажа