Saturday, 6 July 2013

Detect mouse over SWF in Actionscript 3

In this quick tip I will show how to detect if the mouse is currently over the SWF in Actionscript 3. There are two event listeners that need to be used: mouse move and mouse leave. The mouse move listener detect if the mouse is over the swf, and the mouse leave listener detects when the mouse has left the swf.

stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
stage.addEventListener(Event.MOUSE_LEAVE, leaveHandler);

function moveHandler(e:MouseEvent):void
{
 output_txt.text = 'OVER';
}

function leaveHandler(e:Event):void
{
 output_txt.text = 'OUT';
}

Another way to detect mouse inactivity is to use a timer. You can use a Boolean to check if the mouse is currently over the swf like this.

var isOver:Boolean = false;

stage.addEventListener(MouseEvent.MOUSE_MOVE, moveHandler);
stage.addEventListener(Event.MOUSE_LEAVE, leaveHandler);

var timer:Timer = new Timer(5000);
timer.addEventListener(TimerEvent.TIMER, timerHandler);
timer.start();

function moveHandler(e:MouseEvent):void
{
 isOver = true;
 
 timer.reset();
 timer.start();
 
 output_txt.text = 'OVER';
}

function leaveHandler(e:Event):void
{
 isOver = false;
 
 timer.reset();
 
 output_txt.text = 'OUT';
}

function timerHandler(e:TimerEvent):void
{
 timer.reset();
}

0 comments:

  COPYRIGHT © 2014 · ILIKE2FLASH · Theme by Ourblogtemplates

Back to TOP