Flash tutorials
Home 3D (2) Actionscripting (65) Animation (100) Audio (4) Drawing (7) Full flash sites (4) Getting Started (15) Navigation (25) Special Effects (52) Text Effects (38)

Find out mouse position

5.9.2006, 7:47    Total views: 32045

In this tutorial I will show you how to get mouse position information. I know this is not very usefull now but when you comprehend the structure of this exercise you can easely make flash movie in which the some objects following the mouse cursor by reading mouse position coordinates.



Step 1

Create a new flash document, select the first frame, press F9 to open the Action Script Panel and paste this script inside:

this.createTextField("mouse_info", 999, 5, 5, 250, 80);
mouse_info.html = true;
mouse_info.wordWrap = true;
mouse_info.border = true;
mouse_info.autoSize = true;
mouse_info.selectable = false;


var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
var table_str:String = "";
table_str += "My mouse position:  \t"+"x:"+_xmouse+"\t"+"y:"+_ymouse+newline;
table_str += "
";
mouse_info.htmlText = table_str;
};

Mouse.addListener(mouseListener);

Test your movie (Ctrl+Enter)

Explanation :

The script:

this.createTextField("mouse_info", 999, 5, 5, 250, 80);
mouse_info.html = true;
mouse_info.wordWrap = true;
mouse_info.border = true;
mouse_info.autoSize = true;
mouse_info.selectable = false;

makes a new text box named „mouse_info“  and sets the text box parameters (width,height,multiline...).

The script :

var mouseListener:Object = new Object();
mouseListener.onMouseMove = function() {
var table_str:String = "";
table_str += "My mouse position:  \t"+"x:"+_xmouse+"\t"+"y:"+_ymouse+newline;
table_str += "
";
mouse_info.htmlText = table_str;
};
Mouse.addListener(mouseListener);

Cathes the mouse position on the x axis and y axis and sends information to the text box which I have created before („mouse_info“). Text box wtites these information on the screen.

We're done!

Enjoy!

Download source file (.fla)

Have questions about this tutorial?
Visit our friendly Community Forums!
Digg it! Add this tutorial to del.icio.us! Furl it! Add this tutorial to reddit! Spurl it! Add this tutorial to technorati!

Top tutorials

1. Advanced full flash site - Part 1
Total views: 221535

2. Water effect
Total views: 166040

3. Photo slide show
Total views: 153641

4. High-tech city animation
Total views: 151459

5. Special Picture Effect
Total views: 146246

Related links