Make your cursor always point to the middle of the screen
August 14th, 2009 by
Adrian
Yesterday I was taking a look at Balsamiq Mockups and liked the way that the mouse always pointed to the centre of the screen when in fullscreen mode. So I thought I’d quickly knock something to together that did this, and share it here.
It’s pretty simple stuff, but handy to have available to copy-and-paste, if you need something similar.
The code is all in the document class …
{
import flash.display.MovieClip;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class Main extends MovieClip
{
private var mouseIsOverStage:Boolean;
private var arrow:Arrow;
public function Main():void
{
arrow = new Arrow();
Mouse.hide();
stage.addEventListener(Event.MOUSE_LEAVE, onStage_MOUSE_LEAVE);
stage.addEventListener(MouseEvent.MOUSE_MOVE, onStage_MOUSE_MOVE);
}
private function onStage_MOUSE_LEAVE(event:Event):void
{
mouseIsOverStage = false;
removeChild(arrow);
}
private function onStage_MOUSE_MOVE(event:MouseEvent):void
{
if (!mouseIsOverStage) {
mouseIsOverStage = true;
onStage_MOUSE_ENTER();
}
arrow.x = stage.mouseX;
arrow.y = stage.mouseY;
var dx:Number = arrow.x - stage.stageWidth/2;
var dy:Number = arrow.y - stage.stageHeight/2;
var radians:Number = Math.atan2(dy, dx);
arrow.rotation = radians * 180/Math.PI;
event.updateAfterEvent();
}
private function onStage_MOUSE_ENTER():void
{
arrow.x = stage.mouseX;
arrow.y = stage.mouseY;
addChild(arrow);
}
}
}
Posted in ActionScript 3.0, Sample Code | No Comments »