Getting the mouse position

You can use the _xmouse and _ymouse properties to find the location of the mouse pointer (cursor) in a SWF file. Each Timeline has an _xmouse and _ymouse property that returns the location of the mouse 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.

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

The following procedures show two ways to get the mouse position.

To get the current mouse position within the main Timeline:

  1. Create two dynamic text boxes, and name them x_pos and y_pos.
  2. Select Window > Development Panels > Actions to open the Actions panel if it is not already visible.
  3. To return the mouse position within the main Timeline, add the following code to any frame in the _level0 SWF file:
    x_pos = _root._xmouse;
    y_pos = _root._ymouse;
    

The variables x_pos and y_pos are used as containers to hold the values of the mouse positions. You could use these variables in any script in your document. In the following onClipEvent() handler, the values of x_pos and y_pos update every time the user moves the mouse.

onClipEvent(mouseMove){
  x_pos = _root._xmouse;
  y_pos = _root._ymouse;
}

To get the current mouse position within a movie clip:

  1. Create a movie clip.
  2. Select the movie clip instance on the Stage. Using the Property inspector, name it myMovieClip.
  3. Select Window > Development Panels > Actions to open the Actions panel if it is not already visible.
  4. Use the movie clip's instance name to return the mouse position within the main Timeline.

    For example, the following statement could be placed on any Timeline in the _level0 SWF file to return the _ymouse position in the myMovieClip instance:

    x_pos = _root.myMovieClip._xmouse
    y_pos = _root.myMovieClip._ymouse
    

    The code returns the _xpos and _ypos of the mouse, relative to the registration point.

  5. Select Control > Test Movie to test the movie.

You can also determine the mouse position within a movie clip by using the _xmouse and _ymouse properties in a clip event, as shown in the following code:

onClipEvent(enterFrame){
  xmousePosition = this._xmouse;
  ymousePosition = this._ymouse;
}

For more information about the _xmouse and _ymouse properties, see MovieClip._xmouse and MovieClip._ymouse.