Custom Cursor from image URL in AS3


/ Published in: ActionScript 3
Save to your folder(s)

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.


Copy this code and paste it in your HTML
  1. package {
  2.  
  3. import flash.display.Sprite;
  4. import flash.display.Loader;
  5. import flash.net.URLRequest;
  6. import flash.events.Event;
  7. import flash.ui.Mouse;
  8.  
  9.  
  10. public class CustomCursor extends Sprite {
  11.  
  12. private var request:URLRequest;
  13. private var cursorLoader:Loader;
  14.  
  15. /* When you create a custom cursor, pass in the URL of the cursor you want to use */
  16. public function CustomCursor(url:String='crosshair_image.png') {
  17. setCursor(url);
  18. }
  19.  
  20. public function setCursor(url:String):void
  21. {
  22. /* Load our image from the url path */
  23. request = new URLRequest(url);
  24. cursorLoader = new Loader();
  25. cursorLoader.load(request);
  26. /* Listen for when the content of the loader is finished loading */
  27. cursorLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, cursorLoaded);
  28. }
  29.  
  30. private function cursorLoaded(event:Event):void
  31. {
  32. /* The width and height of the new cursor */
  33. var w:Number = event.target.width;
  34. var h:Number = event.target.height;
  35.  
  36. /* Center the image on the mouse */
  37. cursorLoader.x = -w/2;
  38. cursorLoader.y = -h/2;
  39.  
  40. /* Hide the default cursor */
  41. Mouse.hide();
  42.  
  43. /* Add the cursor graphic to the stage */
  44. addChild(cursorLoader);
  45.  
  46. /* startDrag keeps the object the same x,y as the mouse cursor */
  47. this.startDrag(true);
  48. }
  49. }
  50. }

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

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.