Detect Mouse Inactivity in Actionscript 3.0

Before we start you need to be familiar with MouseEvent and Timer classes.

var _timer:Timer = new Timer(5000);

function Start():void
{
   _timer.addEventListener(TimerEvent.TIMER, ShowMsg, false, 0, true);
   _timer.start();

   stage.addEventListener(MouseEvent.MOUSE_MOVE, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_DOWN, StopTimer, false, 0, true);
   stage.addEventListener(MouseEvent.MOUSE_UP, StopTimer, false, 0, true);
}

function ShowMsg(e:TimerEvent):void
{
   trace("There is no activity");
}

function StopTimer(e:MouseEvent):void
{
   _timer.stop();
   _timer.start();
}

Start();

The first thing I’ve done is to setup a Timer object to count up to 5 seconds ( you can change that to what ever numer of seconds you want ).

The timer object also has an event listener so we can know when the 5 seconds are over.

I have setup some event listeners that will help detecting any actions of the mouse on the Stage ( MOUSE_DOWN, MOUSE_MOVE and MOUSE_UP )

If during those 5 seconds nothing happens ( mouse is not moved or performs a click ) ShowMsg() function will be fired up and give you a message of inactivity.

If the mouse is moved the StopTimer() function will be activated and reset timers count.

This is a useful and easy way to detect user activity.

Mouse Follower with Angle Rotation

First create an arrow Movie Clip ( make sure the tip of the arrow is on the register point of the Movie Clip ) and add it to the scene and name it “arrow”.

You will also need Caurina Tweener ( but other tweeners can be used also ).


import caurina.transitions.Tweener;

stage.addEventListener(MouseEvent.MOUSE_MOVE, Moving, false, 0, true);

function Moving(e:Event):void
{
var X:Number =  mouseX - arrow.x;
var Y:Number = (mouseY - arrow.y) * -1;

var angle:Number = Math.atan(Y/X)/(Math.PI/180);

Tweener.addTween(arrow ,{x :mouseX, y: mouseY, time:2});

if (X<0) { 		angle += 180; 	} 	

if (Y>=0 && Y<0) {  angle += 360;}

arrow.rotation = (angle*-1) + 90;

}