Task: Follow the Mouse
Now that you know how to get the mouse location, and you know how to set the location of a movie clip, you can combine these two pieces to make a movie where a movie clip follows the cursor around the screen.
-
Create a new Flash movie.
-
Make a simple movie clip. Something like a small circle will do.
-
Attach the following script the to movie clip:
onClipEvent (enterFrame) { this._x = _root._xmouse; this._y = _root._ymouse; }
That's all there is to it. Check out the sample movie 07followthemouse.fla. When you test the movie, the movie clip immediately snaps to the location of the mouse, provided that the mouse is over the test window.
In Hour 13, "Rollovers," we'll look at how to use a similar technique to create custom cursors.