Creating Interaction with ActionScript > Creating complex interactivity > Getting the mouse position |
![]() ![]() ![]() |
Getting the mouse position
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. 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:
1 |
Create two dynamic text boxes and name them |
2 |
Choose Window > 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 |
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:
1 |
Create a movie clip. |
2 |
Select the movie clip instance on the Stage. Using the Property inspector, name it |
3 |
Choose Window > 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 |
|
x_pos = _root.myMovieClip._xmouse y_pos = _root.myMovieClip._ymouse |
|
The code returns the |
|
5 |
Choose 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 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.
![]() ![]() ![]() |