Tips for Using Custom Mouse Cursors in Flash AS3
Custom mouse pointers are pretty easy to implement in ActionScript 3.0 Flash movies. I'll go over the basics first, but if you want to skip to the bits about making it look more professional, be my guest.
Custom Mouse Pointers in Actionscript 3.0
Using your own MovieClips as custom cursors is a two step deal. First you need to hide the default mouse cursor.
import flash.ui.Mouse; Mouse.hide();
Done. Next, you need to add your MovieClip to the stage and have it follow the mouse around. This requires a bit more code:
function init() { Mouse.hide(); // this creates an instance of the library MovieClip with the // name, "MyCursorClass". this contains your mouse cursor art // myCursor = new MyCursorClass(); // you'll want to make sure the child is added above everything // else, possibly in its own container // addChild(myCursor); // respond to mouse move events stage.addEventListener(MouseEvent.MOUSE_MOVE, mouseMoveHandler); } function mouseMoveHandler(evt:MouseEvent):void { // whenever the mouse moves, place the cursor in the same spot myCursor.x = evt.stageX; myCursor.y = evt.stageY; }
That's pretty much it for the basics. Now, whenever a mouse move event is received, the custom MovieClip's position will be updated to match the hidden mouse cursor's position.
What not to do. This basic custom cursor implementation has a few problems that make it unprofessional.
Cleaning Up the Behavior
In the "what not to do" example there are a couple problems in addition to the fact that the cursor itself is pretty unwieldly and, not to mention, ugly.
The first problem is that when the movie first loads, before the user mouses over it, the cursor can be seen in the top left corner. This can be remedied by hiding the custom cursor until the user moves their mouse over the Flash movie.
myCursor = new MyCursorClass(); myCursor.visible = false; ... function mouseMoveHandler(evt:MouseEvent):void { myCursor.visible = true; myCursor.x = evt.stageX; myCursor.y = evt.stageY; }
Now, when the movie first loads, the cursor will be invisible. It will only appear when the user starts moving the mouse around. However, when the user moves the cursor out of the Flash movie, it will still be visible at the last position before they left off. Lucky for us, Flash fires an event when the mouse leaves the stage so we just listen for that and have the handler hide the cursor.
stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler); ... function mouseLeaveHandler(evt:Event):void { myCursor.visible = false; }
The final problem is a pretty important one. In fact, this should probably be part of the "basic" version above. You'll notice in the "what not to do" example that none of the UI components work. The problem is that the custom cursor is blocking all the mouse events so roll over skins don't appear and the component doesn't notice user clicks.
The "nice" version which feels more professional, despite the still-ugly custom mouse cursor.
To fix this, we simply set the mouseEnabled property of the cursor MovieClip to false.
myCursor = new MyCursorClass(); myCursor.visible = false; myCursor.mouseEnabled = false;
...aaand that's it. Building a quality web application is all in the details and using these tips for custom cursors will bring you one step closer.
Delicious
Digg
Reddit
Facebook
Google
Yahoo
Technorati

Comments
I'm using similar code as above and I noticed that the ugly I shaped cursor comes through the TextField if the type is set to "input".
Even with additional code like this (inside the mouseMoveHandler function):
I was wondering if you have the same problem and/or found any solutions to it.
I'm still searching for a solution but haven't found any yet, so I'm wondering if it's my flash player version bugging things up...
Kind regards,
Jip
Many Thanks, Daniel.
This was 100% what I needed. Got me up and running perfectly in 5 minutes. Super stuff. Thanks again.
Sam
Im trying to build application that looks like GoogleMap. Basic "Zoom IN-OUT" and Navigation Tools. And you can drag a movie clip with mouse and keyboard. The interface and codes working right now. But also I wanna use a "dragging hand" instead of pointer cursor.
So this is actually what i was looking for. But I couldnt make it work :( I tried but couldnt handle it :/ im kinda new on AS. Any help? or i would be really happy if you post a fla file?
Thank you so much for this profesional tutorial. Have a nice day.
Post new comment