Tracking the mouse position gives you information about user movement in your movie. This information allows you to tie user behavior to movie events. You can use the _xmouse
and _ymouse
properties to find the location of the mouse pointer (cursor) in a movie. 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 movie 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 main movie.
The following procedures show two ways to get the mouse position.
To get the current mouse position within the main Timeline:
x_pos
and y_pos
._level0
movie: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 code, 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:
myMovieClip
.For example, the following statement could be placed on any Timeline in the _level0
movie 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.
You can also determine the mouse position within a movie clip by using the _xmouse
and _ymouse
properties in a clip event, as in the following code:
onClipEvent(enterFrame){ xmousePosition = _xmouse; ymousePosition = _ymouse; }
For more information about the _xmouse
and _ymouse
properties, see the online ActionScript Dictionary in the Help menu.