Getting Started
This code assumes that you already have a Movie Clip that will be used as a cursor.
To simulate a cursor you must
Mouse Over | Hide the existing cursor and display the new one |
Mouse Out | Restore the original cursor |
Mouse Move | Move the cursor |
Package
I come from a Delphi programming background, so I am following Delphi syntax and design rules. This means that
private var FActiveCursor : DisplayObject; private var FActiveObject : DisplayObject; public function DefineCursor(AnObject:DisplayObject, image:DisplayObject){ SetCursor( image); SetObject(AnObject); } public function SetCursor(image:DisplayObject){ FActiveCursor = image; if (image is InteractiveObject){ // This stops the cursor from flashing // Otherwise, Mouse_Out is called when the mouse moves over the cursor (FActiveCursor as InteractiveObject).mouseEnabled = false; } image.visible = false; // Hides cursors shown on stage } public function SetObject(AnObject:DisplayObject){ AnObject.addEventListener(MouseEvent.MOUSE_OVER, DoMouseOver) AnObject.addEventListener(MouseEvent.MOUSE_OUT, DoMouseOut) FActiveObject = AnObject; // just for test, will search for it later }These are the events
function DoMouseOver(m:MouseEvent){ Mouse.hide(); FActiveObject.addEventListener(MouseEvent.MOUSE_MOVE,DoMouseMove) FActiveObject.stage.addChild(FActiveCursor); // put the cursor on top of everything FActiveCursor.x = m.stageX; FActiveCursor.y = m.stageY; FActiveCursor.visible = true; m.updateAfterEvent(); // This makes the scrolling much smoother } function DoMouseOut(m:MouseEvent){ Mouse.show(); FActiveObject.removeEventListener(MouseEvent.MOUSE_MOVE,DoMouseMove) FActiveCursor.visible = false; } function DoMouseMove(m:MouseEvent){ FActiveCursor.x = m.stageX; FActiveCursor.y = m.stageY; m.updateAfterEvent(); // This makes the scrolling much smoother }
Calling the code
In the following example, target1 and target2 are single frame Movie Clips (so they can have names). cursors is an imported Movie Clip that contains several named cursors, cursors.DoubleVert refers to one of the cursors.
import mcCommon.*; var MouseCursor1: TMouseCursors = new TMouseCursors; var MouseCursor2: TMouseCursors = new TMouseCursors; MouseCursor1.DefineCursor(target1,cursors.DoubleVert); MouseCursor2.DefineCursor(target2,cursors.DoubleHorz);Author: Robert Clemenzi - clemenzi@cpcug.org