Creating Interaction with ActionScript > Getting the mouse position

Getting the mouse position

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.

To see the _xmouse and _ymouse properties within the main Timeline and in a movie clip Timeline, drag the mouse in the movie.

The following statement could be placed on any Timeline in the _level0 movie to return the _xmouse position within the main Timeline:

x_pos = _root._xmouse;

To determine the mouse position within a movie clip, you can use the movie clip's instance name. For example, the following statement could be placed on any Timeline in the _level0 movie to return the _ymouse position in the myMovieClip instance:

y_pos = _root.myMovieClip._ymouse

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

onClipEvent(enterFrame){
	xmousePosition = _xmouse;
	ymousePosition = _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 movie. In the following example, 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;
}

For more information about the _xmouse and _ymouse properties, see their entries in the ActionScript Dictionary.