The web site for Flash ActionScript 3.0 game developers

 
         
   

Custom Cursor

Early in the book, Gary explains how to make a custom cursor. It's a good example to get started, but there are some minor annoyances that can be improved, such as:

  • for a low frame-rate movie, mouse movement jitters
  • when the mouse moves outside the flash movie, the custom cursor is left near the edge
  • after right clicking in the movie, the regular mouse cursor is shown over the custom cursor

After a little research, I made the following custom cursor code, which solves the above problems:

var cursor:Cursor = new Cursor(); //Cursor class comes from library linkage
addChild(cursor);
cursor.visible = false; //hide custom cursor initially
cursor.mouseEnabled = false;
 
stage.addEventListener(Event.MOUSE_LEAVE, hideCursor);
stage.addEventListener(MouseEvent.MOUSE_MOVE, followCursor); //moves cursor by detecting
                                   //mouse movement instead of relying on ENTER_FRAME
Mouse.hide();
 
function hideCursor(e:Event):void {
    cursor.visible = false; //hides custom cursor when mouse leaves flash
    Mouse.show(); //shows regular mouse when user right clicks
}
 
function followCursor(e:MouseEvent):void {
    if (!cursor.visible) cursor.visible = true;
    cursor.x = e.stageX;
    cursor.y = e.stageY;
    Mouse.hide(); //makes sure regular cursor is hidden after right clicking
    e.updateAfterEvent(); //refreshes screen independent of frame rate
}

This code is for placing on the timeline. Make appropriate changes if you use it in a class file.



Copyright Gary Rosenzweig