Home > ActionScript > ActionScript Follow Mouse

ActionScript Follow Mouse

February 26th, 2008 Leave a comment Go to comments

Creating an Movie Clip that follows the mouse around the screen is the initial step in making your own cursors in Flash. First off, create an empty flash file and draw an object on the stage. Right click on this object and convert it into a Movie Clip by clicking on the item Convert to Symbol… as in the following screenshot.

Convert object to a Movie Clip by right clicking on the object and selecting Convert to Symbol...

You will then see the following dialog box on screen.

The Convert to Symbol dialog box

Make sure Movie Clip is selected and click ok. Additionally, you can also select the registration mark to be different from the default of top left.

Click on the object on the stage again and you enter the following code in the ActionScript window. This keeps the object in the same geographical coordinates as the mouse pointer.

onClipEvent(enterFrame){
 this._x = _root._xmouse;
 this._y = _root._ymouse;
}

You should now see something that looks like the following screenshot.

Entering the onClipEvent enter frame event.

Go to Control->Test Movie or press Ctrl+Enter to test the movie. When you move your mouse pointer over the movie window the object you created should follow it. It won’t follow the mouse once it has left the edge of the window because the event we created only looks at the stage area.

Information, services, and products:
Company registration now online