Mouse Location Flash Script

written by: Paulo Caldeira; article published: year 2007, month 04;


In: Root » Computers and technology » Flash » Mouse Location Flash Script

Dutch French Spanish Portuguese Italian German Japanese Chinese Korean Russian Arabic Bookmark and Share this Article

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. 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) {
   trace(_root._xmouse);
   trace(_root._ymouse);
   trace("");
   }

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.

Disclaimer

1) E-articles is not responsible for the information contained by this article as well for any and all copyright infringements by authors and writers. E-articles is a free information resource. If you suspect this article for any copyright infringement, please read the terms of service and contact us to investigate the problem.
2) E-articles is not responsible for inaccuracies, falsehoods, or any other types of misinformation this article may contain and will not be liable for any loss or damage suffered by a user through the user's reliance on the information gained here.

link to this article