Getting the pointer position

You can use the _xmouse and _ymouse properties to find the location of the pointer in a SWF file. These properties could be used, for example, in a map application that gets the values of the _xmouse and _ymouse properties and uses the values to calculate the longitude and latitude of a specific location.

Each timeline has an _xmouse and _ymouse property that returns the location of the pointer within its coordinate system. The position is always relative to the registration point. For the main timeline (_level0), the registration point is the upper left corner. For a movie clip, the registration point depends on the registration point set when the clip was created or its placement on the Stage.

To see the _xmouse and _ymouse properties within the main timeline and a movie clip timeline, run the following SWF file and move your pointer. The updated coordinates on the right reflect the pointer position relative to the registration point of the smaller movie clip. The coordinates on the left reflect the pointer position on the larger SWF file.

The following procedure shows several ways to get the pointer position within the main timeline or within a movie clip.

To get the current pointer position:

  1. Create two dynamic text fields, and name them box1_txt and box2_txt.
  2. Add labels for the text boxes: x position and y position, respectively.
  3. Select Window > Actions to open the Actions panel if it is not already open.
  4. Add the following code to the script pane:
    var mouseListener:Object = new Object();
    mouseListener.onMouseMove = function() {
        // returns the X and Y position of the mouse
        box1_txt.text = _xmouse;
        box2_txt.text = _ymouse;
    };
    Mouse.addListener(mouseListener);
    
  5. Select Control > Test Movie to test the Flash movie. The box1_txt and box2_txt fields show the position of the pointer while you move it over the Stage.

For more information about the _xmouse and _ymouse properties, see _xmouse (MovieClip._xmouse property) and _ymouse (MovieClip._ymouse property) in the ActionScript 2.0 Language Reference.