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 …

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 »

Setting the stage quality in AS3

March 27th, 2008 by Adrian

This is how you set the stage quality using ActionScript 3.0

Old AS1 & AS2 code was …

_quality = "LOW";

New AS3 code is …

stage.quality = StageQuality.LOW;

The setting options are BEST, HIGH, MEDIUM & LOW.

If you are using this inside a class, you will also have to import the ‘StageQuality’ class from within the ‘flash.display’ package…

import flash.display.StageQuality;

Posted in ActionScript 3.0, Flash, Sample Code | 7 Comments »

Detecting key presses in AS3

February 25th, 2008 by Adrian

Keyboard

In ActionScript 3 you can detect key presses on the keyboard using the Stage class. I discovered that the Stage object is not globally accessible. You need to access it through the stage property of a DisplayObject instance. So from within a Class you can check which keys are being pressed using the following code.

package {
    import flash.events.KeyboardEvent;
    import flash.display.MovieClip;

    public class KeyboardTest extends MovieClip {

        private var base:MovieClip;

        public function KeyboardTest($base:MovieClip):void {
            trace("KeyboardTest constructor is being run");
            base=$base;
            base.stage.addEventListener(KeyboardEvent.KEY_DOWN,keyboardListener);
        }

        private function keyboardListener(event:KeyboardEvent):void {
            trace("event.keyCode: " + event.keyCode);
        }
    }
}

Posted in ActionScript 3.0, Sample Code | No Comments »