package mypackage
{
import flash.display.MovieClip;
import flash.display.Stage;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.ui.Mouse;
public class CustomMouse
{
// define a bunch of constants to be used when setting the cursor. In this case, these are all
// abbreviations for constants I used in production code.
public static const NORMAL:String = "NORMAL";
public static const AE:String = "AE";
public static const AL:String = "AL";
public static const AP:String = "AP";
public static const AR:String = "AR";
public static const AT:String = "AT";
public static const MD:String = "MD";
public static const MV:String = "MV";
public static const TA:String = "TA";
public static const TH:String = "TH";
public static const TI:String = "TI";
public static const TL:String = "TL";
public static const TR:String = "TR";
public static const TT:String = "TT";
public static const TV:String = "TV";
public static const ZI:String = "ZI";
public static const ZO:String = "ZO";
private var stage:Stage;
private var current:String;
private var cursor:MovieClip;
private var xOffset:int;
private var yOffset:int;
public function CustomMouse( s:Stage )
{
this.stage = s;
this.Cursor = NORMAL;
this.xOffset = 0;
this.yOffset = 0;
}
public function set Cursor( val:String ):void
{
this.current = val;
if (this.cursor != null)
{
// for a normal cursor, just use the normal one, for better performance
// remove any event listeners you had added
this.stage.removeChild( this.cursor );
this.stage.removeEventListener( MouseEvent.MOUSE_MOVE, mouseMoved );
this.stage.removeEventListener( Event.MOUSE_LEAVE, mouseLeft );
}
if (this.current == NORMAL)
{
Mouse.show();
this.cursor = null;
}
else
{
// Hide the real mouse
Mouse.hide();
switch (this.current)
{
// if needed, I can set the offsets in each case statement
case AE:
this.cursor = new cursorAE();
break;
case AL:
this.cursor = new cursorAL();
break;
case AP:
this.cursor = new cursorAP();
break;
case AR:
this.cursor = new cursorAR();
break;
case AT:
this.cursor = new cursorAT();
break;
case MD:
this.cursor = new cursorMD();
break;
case MV:
this.cursor = new cursorMV();
break;
case TA:
this.cursor = new cursorTA();
break;
case TH:
this.cursor = new cursorTH();
break;
case TI:
this.cursor = new cursorTI();
break;
case TL:
this.cursor = new cursorTL();
break;
case TR:
this.cursor = new cursorTR();
break;
case TT:
this.cursor = new cursorTT();
break;
case TV:
this.cursor = new cursorTV();
break;
case ZI:
this.cursor = new cursorZI();
break;
case ZO:
this.cursor = new cursorZO();
break;
}
// your DisplayObject is above the mouse cursor, and blocks click events, unless you
// turn off mouseEnabled and mouseChildren
this.cursor.mouseEnabled = false;
this.cursor.mouseChildren = false;
// cacheAsBitmap is supposed to give you slightly better performance
this.cursor.cacheAsBitmap = true;
// set initial position
this.cursor.x = this.stage.mouseX + this.xOffset;
this.cursor.y = this.stage.mouseY + this.yOffset;
this.stage.addChild( this.cursor );
// catch MouseMove to follow the Mouse
this.stage.addEventListener( MouseEvent.MOUSE_MOVE, mouseMoved );
// catch MouseLeave so that you can make the custom mouse disappear. If you don't do
// this, when the mouse leaves the window, you'll see the custom mouse on the screen
this.stage.addEventListener( Event.MOUSE_LEAVE, mouseLeft );
}
}
public function get Cursor():String
{
return this.current;
}
public function mouseMoved( e:MouseEvent ):void
{
// no need to check that this.cursor is not null, because we remove the eventListener if it is.
this.cursor.visible = true;
this.cursor.x = e.stageX + this.xOffset;
this.cursor.y = e.stageY + this.yOffset;
// this also is supposed to give you better performance.
e.updateAfterEvent();
}
public function mouseLeft( e:Event ):void
{
this.cursor.visible = false;
}
}
}
import mypackage.CustomMouse;
// ...
var customMouse:CustomMouse
// ...
function addedToStage( e:Event ):void
{
// ...
customMouse = new CustomMouse( stage );
// ...
}
// ...
customMouse.cursor = CustomMouse.AE;
public function mouseMoved():void
{
// no need to check that this.cursor is not null, because we remove the eventListener if it is.
this.cursor.visible = true;
this.cursor.x = this.stage.mouseX + this.xOffset;
this.cursor.y = this.stage.mouseX + this.yOffset;
}
package mypackage
{
import mx.core.Application;
import mx.managers.CursorManager;
public class CustomMouse
{
public static const NORMAL:String = "NORMAL";
public static const AE:String = "AE";
public static const AL:String = "AL";
public static const AP:String = "AP";
public static const AR:String = "AR";
public static const AT:String = "AT";
public static const MD:String = "MD";
public static const MV:String = "MV";
public static const TA:String = "TA";
public static const TH:String = "TH";
public static const TI:String = "TI";
public static const TL:String = "TL";
public static const TR:String = "TR";
public static const TT:String = "TT";
public static const TV:String = "TV";
public static const ZI:String = "ZI";
public static const ZO:String = "ZO";
[Embed(source="cursorImages/cursorAE.png")]
private var CursorAE:Class;
[Embed(source="cursorImages/cursorAL.png")]
private var CursorAL:Class;
[Embed(source="cursorImages/cursorAP.png")]
private var CursorAP:Class;
[Embed(source="cursorImages/cursorAR.png")]
private var CursorAR:Class;
[Embed(source="cursorImages/cursorAT.png")]
private var CursorAT:Class;
[Embed(source="cursorImages/cursorTA.png")]
private var CursorTA:Class;
[Embed(source="cursorImages/cursorMD.png")]
private var CursorMD:Class;
[Embed(source="cursorImages/cursorTH.png")]
private var CursorTH:Class;
[Embed(source="cursorImages/cursorTI.png")]
private var CursorTI:Class;
[Embed(source="cursorImages/cursorTL.png")]
private var CursorTL:Class;
[Embed(source="cursorImages/cursorTR.png")]
private var CursorTR:Class;
[Embed(source="cursorImages/cursorTT.png")]
private var CursorTT:Class;
[Embed(source="cursorImages/cursorMV.png")]
private var CursorM:Class;
[Embed(source="cursorImages/cursorTV.png")]
private var CursorTV:Class;
[Embed(source="cursorImages/cursorZI.png")]
private var CursorZI:Class;
[Embed(source="cursorImages/cursorZO.png")]
private var CursorZO:Class;
private var current:String;
public function CustomMouse()
{
this.Cursor = NORMAL;
}
public function set Cursor( val:String ):void
{
this.current = val;
// this is here to allow me to debug through the Flash IDE - the cursor won't
// change, but I'll still have my functionality
if (Application.application == null)
{
return;
}
CursorManager.removeCursor( CursorManager.currentCursorID );
switch (this.current)
{
case AE:
CursorManager.setCursor( CursorAE );
break;
case AL:
CursorManager.setCursor( CursorAL );
break;
case AP:
CursorManager.setCursor( CursorAP );
break;
case AR:
CursorManager.setCursor( CursorAR );
break;
case AT:
CursorManager.setCursor( CursorAT );
break;
case MD:
CursorManager.setCursor( CursorMD );
break;
case MV:
CursorManager.setCursor( CursorMV );
break;
case TA:
CursorManager.setCursor( CursorTA );
break;
case TH:
CursorManager.setCursor( CursorTH );
break;
case TI:
CursorManager.setCursor( CursorTI );
break;
case TL:
CursorManager.setCursor( CursorTL );
break;
case TR:
CursorManager.setCursor( CursorTR );
break;
case TT:
CursorManager.setCursor( CursorTT );
break;
case TV:
CursorManager.setCursor( CursorTV );
break;
case ZI:
CursorManager.setCursor( CursorZI );
break;
case ZO:
CursorManager.setCursor( CursorZO );
break;
}
}
public function get Cursor():String
{
return this.current;
}
}
}
Error: No class registered for interface ‘mx.managers::ICursorManagIn the end, the only solution I could find for this was to create a Flex application that loads the compiled .swf. The mxml code for this is simple, and free - (all you need to do is download and setup FlashDevelop to go with your newly downloaded Flex sdk, from http://www.flashdevelop.org/community/viewforum.php?f=11). Create a new mxml project, and simply put this in your Main.mxml:er’.
at mx.core::Singleton$/getInstance()
at mx.managers::CursorManager$/get impl()
<?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml">
<mx:SWFLoader id="loader2" source="MyFlashApp.swf"></mx:SWFLoader>
</mx:Application>
import mx.core.Application;
// ...
if (Application.application == null)
{
// do not-loaded-in-Flex stuff
}
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)