23May

Changing the Mouse Cursor

No comments
package {
    import flash.display.Sprite;
	import flash.display.DisplayObject;
    import flash.ui.Mouse;
    import flash.events.Event;

	public class CustomMouseCursor extends Sprite {
		private var stageSprite;
		private var cursor:CustomCursor;

         private var gutter:uint = 10;

        public function CustomMouseCursor() {
			stageSprite = new Sprite();
			addChild ( stageSprite );
			stageSprite.width = stage.stageWidth;
			stageSprite.height = stage.stageHeight;

			stageSprite.addEventListener(Event.ENTER_FRAME, mouseOverHandler);
            cursor = new CustomCursor();
            addChild(cursor);
        }

        private function mouseOverHandler(e:Event):void {
            Mouse.hide();
            stageSprite.addEventListener(Event.ENTER_FRAME, mouseMoveHandler);
        }

        private function mouseOutHandler(e:Event):void {
            Mouse.show();
           	stageSprite.removeEventListener(Event.ENTER_FRAME, mouseMoveHandler);
            cursor.visible = false;
        }

        private function mouseMoveHandler(e:Event):void {
            cursor.x = mouseX;
            cursor.y = mouseY;
            cursor.visible = true;
        }
    }
}

import flash.display.Shape;
class CustomCursor extends Shape {
    var bgColor:uint = 0x336699;
    var size:uint = 10;

    public function CustomCursor() {
        visible = false;
        draw();
    }

    private function draw():void {
        graphics.clear();
        graphics.beginFill(bgColor);
        graphics.drawRect(0, 0, size, size);
        graphics.endFill();
    }
}

Share/Save/Bookmark

Categories: Actionscript 3

Friday, May 23rd, 2008 at 12:53 pm and is filed under Actionscript 3. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

Leave a reply