Skip to content


Changing the Mouse Cursor

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();
    }
}

Posted in Actionscript 3.


4 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Patrick says

    It seems like every time I google an AS problem, your site comes up and has the solution I need. I remember your picture up in the corner and every time I say “Damn dude, you saved my butt again.”

  2. admin says

    Glad to hear these little movies are able to help out!

    Morgan

  3. kishi says

    but why use shape for that? is it faster than just moving sprite around?

  4. oscar says

    isn’t there a short code with the same results..??



Some HTML is OK

or, reply to this post via trackback.