Search Our Web Design Blog...

Wednesday, May 30, 2012

A customized cursor

 To make a custom cursor follow these steps:

The code (based on a movie clip named MyCursorClass with a Linkage set to MyCursorClass:




import flash.display.Sprite;
import flash.display.StageAlign;
import flash.display.StageScaleMode;
import flash.events.Event;
import flash.events.MouseEvent;

var myCursor:Sprite;

stage.align = StageAlign.TOP_LEFT;
stage.scaleMode = StageScaleMode.NO_SCALE;

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();
    myCursor.mouseEnabled = false;
    myCursor.visible = false;
   
    // 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);
    stage.addEventListener(Event.MOUSE_LEAVE, mouseLeaveHandler);
}

function mouseMoveHandler(evt:MouseEvent):void
{
    // whenever the mouse moves, place the cursor in the same spot
    myCursor.visible = true;
    myCursor.x = evt.stageX;
    myCursor.y = evt.stageY;
}

function mouseLeaveHandler(evt:Event):void
{
    myCursor.visible = false;
}

init();

 Kind of confusing I know... so here is an easier but less cool way to do the same thing:





if you do not need your cursor to be very precise:

name your instance name for your new cursor movieclip "cursor"
then add this code to frame 1 of your main timeline:


Mouse.hide();
stage.addEventListener(MouseEvent.MOUSE_MOVE,follow);
function follow(evt:MouseEvent){
 cursor.x = mouseX;
 cursor.y = mouseY;
}
 
 
 
Now all you need to do is double click on your movie clip and
move the object to align with the crosshair (this is the registration
point for your cursor).

1 comment:

  1. Its such a fantastic post about how to make a custom cursor. In this post very nicely describe each and every steps for make a custom cursor.
    indesign training

    ReplyDelete