Return to Snippet

Revision: 32808
at October 2, 2010 09:23 by bshantz


Initial Code
package  {
	
	import flash.display.Sprite;
	import flash.display.Loader;
	import flash.net.URLRequest;
	import flash.events.Event;
	import flash.ui.Mouse;
	
	
	public class CustomCursor extends Sprite {
		
		private var request:URLRequest;
		private var cursorLoader:Loader;
		
		/*	When you create a custom cursor, pass in the URL of the cursor you want to use	*/
		public function CustomCursor(url:String='crosshair_image.png') {
			setCursor(url);
		}
		
		public function setCursor(url:String):void
		{
			/*	Load our image from the url path	*/
			request = new URLRequest(url);
			cursorLoader = new Loader();
			cursorLoader.load(request);
			/*	Listen for when the content of the loader is finished loading	*/
			cursorLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, cursorLoaded);
		}
		
		private function cursorLoaded(event:Event):void
		{
			/*	The width and height of the new cursor	*/
			var w:Number = event.target.width;
			var h:Number = event.target.height;
			
			/*	Center the image on the mouse	*/
			cursorLoader.x = -w/2;
			cursorLoader.y = -h/2;
			
			/*	Hide the default cursor	*/
			Mouse.hide();
			
			/*	Add the cursor graphic to the stage	*/
			addChild(cursorLoader);
			
			/*	startDrag keeps the object the same x,y as the mouse cursor	*/
			this.startDrag(true);
		}
	}
}

Initial URL
http://www.commandreturn.com/demos/customCursorDemo/customCursorDemo.swf

Initial Description
This is a class I made for using custom cursors without having to import them into an FLA.  Just pass in the URL of the cursor image and you're good to go.

Initial Title
Custom Cursor from image URL in AS3

Initial Tags

                                

Initial Language
ActionScript 3