AS3 CursorManager

CursorManager is simple as3 class that lets you change cursor at runtime.
Example uses also Event.MOUSE_LEAVE event to hide/remove custom cursor when mouse leaves flash display area(with combination of MouseEvent.MOUSE_MOVE to show back cursor).

Example:

Get Adobe Flash player

CursorManager.as

Actionscript:
  1. package com.abrahamyan.util
  2. {
  3.     import flash.display.DisplayObject;
  4.     import flash.display.DisplayObjectContainer;
  5.     import flash.display.InteractiveObject;
  6.     import flash.display.Stage;
  7.     import flash.ui.Mouse;
  8.     import flash.events.MouseEvent;
  9.    
  10.     /**
  11.      * ...
  12.      * @author Armen Abrahamyan
  13.      */
  14.     public class CursorManager
  15.     {
  16.         private static var _cursor:DisplayObject;
  17.         private static var _xOffset:Number;
  18.         private static var _yOffset:Number;
  19.         private static var _root:Stage
  20.        
  21.         public function CursorManager():void
  22.         {
  23.             throw new Error("no need to call constructor, use static methods CursorManager.init(), CursorManager.setCursor(),CursorManager.removeCursor(),CursorManager.destroy() " );
  24.         }
  25.         /**
  26.          * sets stage path
  27.          * @param   pRoot
  28.          */
  29.         public static function init(pRoot:Stage):void
  30.         {
  31.             _root = pRoot;
  32.         }
  33.         /**
  34.          *  sets display object as cursor ( removes prev cursor automatically)
  35.          * @param   pCursor  new cursor
  36.          * @param   pXoffset x offset from mouse position
  37.          * @param   pYoffset y offset from mouse position
  38.          */
  39.         public static function setCursor(pCursor:DisplayObject, pXoffset:Number = 0, pYoffset:Number = 0):void
  40.         {
  41.             if (!_root)
  42.             {
  43.                 throw new Error("set root using init(pRoot)");
  44.             }
  45.             if (_cursor)
  46.             {
  47.                 removeCursor();
  48.             }
  49.             Mouse.hide();
  50.             _cursor = pCursor;
  51.             if (_cursor is InteractiveObject)
  52.             {
  53.                 InteractiveObject(_cursor).mouseEnabled = false;
  54.                 if (_cursor is DisplayObjectContainer)
  55.                 {
  56.                     DisplayObjectContainer(_cursor).mouseChildren = false;
  57.                 }
  58.             }
  59.            
  60.             _xOffset = pXoffset;
  61.             _yOffset = pYoffset;
  62.             _cursor.x = _root.mouseX + _xOffset;
  63.             _cursor.y = _root.mouseY + _yOffset;
  64.             _root.addChild(_cursor);
  65.             _root.addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  66.            
  67.         }
  68.         /**
  69.          * update cursor position
  70.          * @param   e
  71.          */
  72.         private static function onMouseMove(e:MouseEvent):void
  73.         {
  74.             _cursor.x = _root.mouseX + _xOffset;
  75.             _cursor.y = _root.mouseY + _yOffset;
  76.             e.updateAfterEvent();
  77.         }
  78.         /**
  79.          * brings cursor on top of display list in case u add another displayobjext on stage
  80.          */
  81.         public static function bringToFront():void
  82.         {
  83.             if (_cursor)
  84.             {
  85.                 _root.addChild(_cursor);
  86.             }
  87.         }
  88.         /**
  89.          * removes cursor created by last setCursor() method, resotres original system cursor
  90.          */
  91.         public static function removeCursor():void
  92.         {
  93.            
  94.             if (!_cursor)
  95.             {
  96.                 return;
  97.             }
  98.             _root.removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
  99.             _root.removeChild(_cursor);
  100.             _cursor = null;
  101.             Mouse.show();
  102.            
  103.         }
  104.         /**
  105.          * works exactly same way as removeCursor()
  106.          */
  107.         public static function destroy():void
  108.         {
  109.             if (_cursor)
  110.             {
  111.                 removeCursor();
  112.             }
  113.         }
  114.        
  115.     }
  116.    
  117. }

Download source files

Releated Posts


7 Responses to “AS3 CursorManager”


  1. 1 andrusha-aviator

    Thanks!
    Excelent example.

  2. 2 Pablo Davi

    Great work and so much usefull.
    XD

  3. 3 cddin

    great work!!!

  4. 4 Abdul Qabiz

    Very neat stuff, long time back I wrote something similar:-

    http://code.google.com/p/as3httpclient/source/browse/trunk/src/com/abdulqabiz/utils/?r=6

  5. 5 duaiCH

    Ye,so excelent!

  6. 6 ynk

    it’s adding a new displayobject to the displaylist, directly on the stage… what if an other object is added after ? will the cursor be hidden by that new object ?

  7. 7 admin

    2ynk: there is a public, static method bringToFront(), once you add another displayobject, you can use CursorManager.bringToFront() to bring cursor front.

Leave a Reply