Posted By

allnatural on 03/13/09


Tagged

detection mouse


Versions (?)

Who likes this?

9 people have marked this snippet as a favorite

vip4lyf
MacEvelly
vkord
flashinit
alptugan
davidino1978
zoo
albertomori
letmein_xxi


Detect direction mouse is moving


 / Published in: ActionScript 3
 

URL: www.kirupa.com

Simple script to determine what direction the mouse is moving.

  1. function Start() {
  2.  
  3. stage.addEventListener(MouseEvent.MOUSE_MOVE, CheckDirection);
  4.  
  5. }
  6. Start();
  7.  
  8. var prevX=0;
  9. var prevY=0;
  10. var curX=0;
  11. var curY=0;
  12.  
  13.  
  14. var dirX:String="";
  15. var dirY:String="";
  16.  
  17. function CheckDirection(e:MouseEvent) {
  18.  
  19.  
  20. trace("X movement: " + GetHorizontalDirection() + ", Y movement: " + GetVerticalDirection());
  21.  
  22. e.updateAfterEvent();
  23.  
  24. }
  25.  
  26. function GetHorizontalDirection():String {
  27.  
  28. prevX=curX;
  29. curX=stage.mouseX;
  30.  
  31. if (prevX>curX) {
  32.  
  33. dirX="left";
  34.  
  35. } else if (prevX < curX) {
  36.  
  37. dirX="right";
  38.  
  39. } else {
  40.  
  41. dirX="none";
  42.  
  43. }
  44.  
  45. return dirX;
  46.  
  47. }
  48.  
  49. function GetVerticalDirection():String {
  50.  
  51. prevY=curY;
  52. curY=stage.mouseY;
  53.  
  54. if (prevY>curY) {
  55.  
  56. dirY="up";
  57.  
  58. } else if (prevY < curY) {
  59.  
  60. dirY="down";
  61.  
  62. } else {
  63.  
  64. dirY="none";
  65.  
  66. }
  67.  
  68. return dirY;
  69.  
  70. }

Report this snippet  

You need to login to post a comment.