Pages

Monday, January 4, 2010

drag & enlarge mouse cursor

While working on a game project, I was trying to create an effect where I could enlarge a mouse cursor by simply dragging it.

Script which makes it work:

pointer.enabled = false;
Mouse.hide();

stage.addEventListener(MouseEvent.MOUSE_MOVE, moveLight);
stage.addEventListener(MouseEvent.MOUSE_DOWN, dragLight);
stage.addEventListener(MouseEvent.MOUSE_UP, dropLight);
//Proper mouse cursor function.
function moveLight(evt:MouseEvent):void
{
pointer.x = this.mouseX;
pointer.y = this.mouseY;
}

function dragLight(evt:MouseEvent):void
{
//Stop cursor from following for a moment.
stage.removeEventListener(MouseEvent.MOUSE_MOVE, moveLight);
//If the mouse move out of the MovieClip, start rescaling.
if (mouseX >= pointer.x && mouseY >= pointer.y && mouseX <= pointer.x &&
mouseY <= pointer.y)
{
pointer.addEventListener(Event.ENTER_FRAME, rescaleLight);
}
}
//When user stop holding on left click, resume the movement of the cursor,
stop dragging and scaling the cursor.
function dropLight(evt:MouseEvent):void
{
stage.addEventListener(MouseEvent.MOUSE_MOVE, moveLight);
pointer.removeEventListener(Event.ENTER_FRAME, rescaleLight);
}

function rescaleLight(evt:Event):void
{
//Number variables which represents the distance between the mouse and cursor.
var dragSizeX:Number = pointer.x - mouseX;
var dragSizeY:Number = pointer.y - mouseY;
//If the distance between the mouse and the cursor is a negative value,
multiply a negative number to make it positive. Else, no need to.
if (dragSizeX >= 0)
{
pointer.width = dragSizeX * 2;
}
else if (dragSizeX <= 0)
{
pointer.width = dragSizeX * -2;
}
if (dragSizeY >= 0)
{
pointer.height = dragSizeY * 2;
}
else if (dragSizeY <= 0)
{
pointer.height = dragSizeY * -2;
}
}


Explanations are provided inside the script box above, in black font.

No comments: