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)