AS3 Detect if mouse is over the stage


/ Published in: ActionScript 3
Save to your folder(s)

This is a pretty robust mouse detection technique. Use the mouseIsOver variable when deciding whether to do something (ie animation)


Copy this code and paste it in your HTML
  1. var mouseIsOver:Boolean = false;
  2. stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
  3.  
  4. function mouseMoved(evt:Event):void
  5. {
  6. mouseIsOver = true;
  7. stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
  8. stage.addEventListener(Event.MOUSE_LEAVE, mouseLeft);
  9. }
  10.  
  11. function mouseLeft(evt:Event):void
  12. {
  13. mouseIsOver = false;
  14. stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeft);
  15. stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
  16. }

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.