Home > Articles > Graphics & Web Design > Dreamweaver & Flash

  • Print
  • + Share This
This chapter is from the book

Mouse Location

Not only can you get the location of a movie clip on the screen, you can even get the location of the mouse, also known as the cursor.


What is the difference between the mouse and the cursor? The mouse is the physical device attached to your computer. You may even have a track pad or tablet instead. The cursor is the graphic that moves around the screen as you move your mouse. So, technically, cursor is the term I should be using here. However, ActionScript uses the term mouse in its keywords. I will therefore use mouse and cursor interchangeably.

Two special properties of the Flash player tell you the horizontal and vertical positions of the cursor on the screen. These are the _xmouse and _ymouse properties.

But what are they properties of? If used by themselves, _xmouse and _ymouse are properties of the object they are contained in. So if you use them in the main timeline, they are root properties. If you use them in a movie clip, they are movie clip properties.

What's the difference? Well, _xmouse and _ymouse measure the mouse location from registration point of the object. So if you are using the root properties, you get the location of the mouse from the upper-left corner of the movie. If you use them inside a movie clip, you get the mouse location from the center of the movie clip.

In most cases, you will want these properties as they relate to the main movie. To ensure this, you can use _root._xmouse and _root._ymouse.

Here is the code inside the example movie 07mouse.fla. It contains a movie clip with this script on it. In every frame that passes, the script will write the x and y location of the mouse to the Output window, followed by a blank line to separate the pairs of numbers.

onClipEvent (enterFrame) {

When you run this movie, you will see the pairs of numbers stream through the Output window. Move the mouse around and watch the numbers change. Bring the cursor up near the upper-left corner of the screen to see them get close to 0, 0 and then to the lower-right to see them get close to 550, 400.


Notice that if you move the cursor outside the Flash test window, the values of _xmouse and _ymouse don't change. If you move the cursor from the center of the movie to a point outside the movie fast enough, the old value stays until the cursor re-enters the active area. This is an unfortunate fact of life with Flash and something that you will have to plan for when using _xmouse and _ymouse.

  • + Share This
  • 🔖 Save To Your Account