Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 3

mouseX and mouseY properties to track mouse location

Flash has built-in properties mouseX and mouseY which let you know the current location of the mouse pointer. This can be used in Flash games for example. To query these values on a continuous basis, you can set up an event listener that fires on every frame.

Download the FLA file

var myTextField:TextField = new TextField();
var myTextFormat:TextFormat = new TextFormat("Arial",20,0,true);
myTextField.defaultTextFormat = myTextFormat;

myTextField.text = "test";
myTextField.wordWrap = true; // wordWrap required to put text on two lines
/*myTextField.multiline = true; // works even when I comment out multiline */
myTextField.border = true;
myTextField.background = true;
myTextField.backgroundColor = 0xffffff;
myTextField.selectable = false;
myTextField.x = 50;
myTextField.y = 50;
myTextField.height = 60;
myTextField.width = 150;
addChild(myTextField);

addEventListener(Event.ENTER_FRAME, showMouseLocation);

function showMouseLocation(myEvent:Event)
{
myTextField.text = "mouseX:" + mouseX.toString() + " " + "mouseY:" + mouseY.toString();
}


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM