Return to Snippet

Revision: 25983
at April 15, 2010 06:02 by weavermedia


Updated Code
var mouseIsOver:Boolean = false;
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);

function mouseMoved(evt:Event):void
{
  mouseIsOver = true;
  stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
  stage.addEventListener(Event.MOUSE_LEAVE, mouseLeft);
}

function mouseLeft(evt:Event):void
{
  mouseIsOver = false;
  stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeft);
  stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
}

Revision: 25982
at April 15, 2010 06:00 by weavermedia


Initial Code
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);

function mouseMoved(evt:Event):void
{
mouseIsOver = true;
stage.removeEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeft);
}

function mouseLeft(evt:Event):void
{
mouseIsOver = false;
stage.removeEventListener(Event.MOUSE_LEAVE, mouseLeft);
stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoved);
}

Initial URL

                                

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

Initial Title
AS3 Detect if mouse is over the stage

Initial Tags

                                

Initial Language
ActionScript 3