Make your cursor always point to the middle of the screen

August 14th, 2009 by Adrian Parr

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 …

package
{
    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);
        }
       
    }

}

Download the source files here

Posted in ActionScript 3.0, Sample Code | No Comments »

Hiding the cursor in AS3

April 1st, 2008 by Adrian Parr

To hide the mouse cursor using AS3 use this …

package {
    import flash.display.Sprite;
    import flash.ui.Mouse;

    public class Main extends Sprite
    {
        public function Main():void
        {
            Mouse.hide();
        }
    }
}

Posted in ActionScript 3.0, Flash, Sample Code | 1 Comment »