Posted By

weavermedia on 04/15/10


Tagged

as3mouse


Versions (?)

Who likes this?

4 people have marked this snippet as a favorite

weavermedia
socialflasher
davidino1978
tspitzr


AS3 Detect if mouse is over the stage


 / Published in: ActionScript 3
 

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

  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  

You need to login to post a comment.