More Mouse events

the _xmouse and _ymouse properties
The _xmouse and _ymouse properties return respectively the x and the y coordinate of the mouse position in relation to the registration point of the Instance it is referring to. These two properties exist for the following classes : TexField, Button, MovieClip, Video, Screen. These properties will return the correct values in relation to the Instance the method is assigned to, for example _root._xmouse will return the x position of the mouse in relation to the root, myMovieClip._xmouse will return the x position of the mouse in relation to the myMovieClip position etc...

Note that when you create a movie clip or a button you can specify if you want the registration point to be set in the center or in the top left corner (pic 1)

pic 1

The example below demonstrates how the x and y coordinates are retrieved in relation to the _root and displayed in their corresponding text fields.
onMouseMove = function() {
xmouse_value.text = _root._xmouse;
ymouse_value.text = _root._ymouse;
updateAfterEvent();
}
In the example above you will notice that I use the built in listener onMouseMove to update the x and y position as soon as the mouse is moved over the movie. I am using updateAfterEvent in that bit of code to force the player to refresh the screen for the simple reason that if we rely on the 12 frames per second refresh rate it won't be high enough to display the values accurately.

The second example below is very similar but this time it returns the mouse position in relation to the red square registration point.
onMouseMove = function() {
xmouse_value.text = _root.square._xmouse;
ymouse_value.text = _root.square._ymouse;
updateAfterEvent();
}
More Mouse events

You now should have a good understanding of the _xmouse and _ymouse properties. You surely understood that the onMouseMove event is triggered each time the mouse move on the screen and that the code encapsulated in the function will be executed each time. You will be glad to know that in addition to the the onMouseMove event we have access to 3 other useful mouse events which are : onMouseUp, onMouseDown and onMouseWheel.
Let's have a look at the movie below and to the code associated with it :
onMouseMove = function () {
feedback.text = "You are moving your mouse";
};

onMouseDown = function () {
feedback.text = "You pressed the left mouse button";
};

onMouseUp = function () {
feedback.text = "You released your left mouse button";
};
myListener = new Object();
myListener.onMouseWheel = function () {
feedback.text = "You are playing with your mouse wheel";
};

Mouse.addListener(myListener);
The event onMouseDown is fired as soon as you press the left mouse button, The event onMouseup is fired as soon as you release the left mouse button. You will notice that for the last event onMouseWheel you need to define a listener object, also for the onMouseWheel to work correctly you need to click on the movie (or the text field) first to give it the focus.

Continue to next page ...

More Mouse events Rating: 4.5 Diposkan Oleh: Catatanku