• Looking for something?

Custom Cursor Class in AS2

I have made a simple yet quite useful custom cursor class in this case is in AS2. All that is needed is a movieclip that will take use of it. This also doesn’t make the mouse flicker with constant Mouse.hide() calls, it stays hidden only when on focus with the swf. If you know how to extend a movieclip this would look very very simple and easy.

Custom Cursor Class - sample swf

The class extends the movieclip class therefore inherits all of a movieclip’s properties, methods and events. With a cursor all we would practically need is the onMouseMove event. See this custom cursor on the work from my game, Puttbase.

class MCCursor extends MovieClip {
	static var cur:MCCursor;
 
	var frame:Object;
	var onMouseMove2:Function;
 
	function MCCursor(){
		Mouse.hide();
		gotoAndStop(1);
		if ( cur ) {
			cur.removeMovieClip();
		}
		// singleton class
		cur = this;
		// swap to a very high but still safe depth
		swapDepths( 1048031 );
	}
 
	function onMouseMove(){
		_x += _xmouse;
		_y += _ymouse;
		onMouseMove2();
	}
}

Basically this is all we would need. The class is singleton (restricted to only one instance) so the instanced movieclip is stored on the static variable cur. First create a movieclip, name it whatever you want, set a linkage identifier and, most importantly, have “MCCursor” as its class (see figure below). The class may vary if you’re using a separate folder to contain all your classes but I’ll leave explaining that on another time.

Figure screenshot

Figure screenshot

To instantiate, either leave the cursor movieclip on stage during design time, where the class is instanced when the movieclip appears on stage, or attach them during run time like this:

var cur = _root.attachMovie( 'mcCursor', 'cur', _root.getNextHighestDepth() );

Now to add some other functions to make some jobs and our life easier:

This hides the mouse pionter again when the swf regains focus:

var onMouseDown2:Function;
 
function onMouseDown(){
	Mouse.hide();
	/**
	* force update position, not totally
	* necessary but might be useful somehow
	* */
	this.onMouseMove();
	onMouseDown2();
}

If you want to have your own onMouseDown event function you can use onMouseDown2 instead, so to not overwrite this method, do something like:

MCCursor.cur.onMouseDown2 = function(){
	//do what you want
}

To handle a better gotoAndStop function:

var frame:Object;
/**
* f parameter can be a number or a string (frame label),
* it will be stored on frame variable regardless
* if it is a valid frame from the movieclip or not
* */
function gotoAndStop( f:Object ) {
	super.gotoAndStop( f );
	frame = f;
}
/**
* f is a numeric value or string of the made frame label
* */
static function gotoFrame( f:Object ) {
	if ( !cur ) return;
	cur.gotoAndStop( f );
}
/**
* set getFrameNumber parameter to true to force returning the
* actual numberic value of the current frame of the movieclip
* */
static function getFrame( getFrameNumber:Boolean ):Object {
	if ( !cur ) return null;
	if ( getFrameNumber )
		return cur._currentframe;
	else
		return cur.frame;
}

For convenience, use the this static function to set the frame of the cursor and can be accessed anywhere (MCCursor class must be imported on the class to recognize it):

MCCursor.gotoFrame( f );

…have these on the class then it’s good to be used.

Grab the actual Class AS file here which also quite commented.

This is a v0.1 so it is a simple but is really expandable class. I’ll update this with new methods and features when something comes up and is necessary. An AS3 version will also be made soon.


Other Keywords:

  • Change Actionscript 2 mouse cursor
  • Flash mouse pointer
  • Custom mouse pointer
  • Personal mouse cursor
  • Replace default cursor with custom mouse pointer
Classic (7 comments) | Fb Comments
Share your thoughts, socialize!