Wednesday, October 24, 2012

Get Mouse Position in flex 4.5

 <?xml version="1.0" encoding="utf-8"?>
<mx:Application xmlns:mx="http://www.macromedia.com/2003/mxml" horizontalAlign="left" 
  mouseMove="onMouseMove(event)"
  mouseDown="onMouseDown(event)">
<mx:Script><![CDATA[
 //tests mousedown to see if it is over Web Access button
 private var giMouseXLast:Number = 0;
 private var giMouseYLast:Number = 0;
 private function onMouseDown(oEvent:Object):Void
 {
  var iMouseX:Number = oEvent.target.mouseX;
  var iMouseY:Number = oEvent.target.mouseY;
  if (iMouseX > 200 && iMouseX < 400 && iMouseY > 100 && iMouseY <200)  {   //mousedown is over magic spot
   alert("You hit the magic spot");
  }
  lblLastMouseDownCoord.text = "(" + iMouseX + "," + iMouseY + ")"
 }

 //sets global variables when mouse moves
 private function onMouseMove(oEvent:Object):Void
 {
  giMouseXLast = oEvent.target.mouseX;
  giMouseYLast = oEvent.target.mouseY;

 }
]]></mx:Script>
 <mx:HBox>
  <mx:Label id="lblLastMouseDown" text="Last Mouse Down"  color="#FFFFFF" fontSize="18" />
  <mx:Label id="lblLastMouseDownCoord" text="" color="#FFFFFF" fontSize="18" />
 </mx:HBox>

 <mx:HBox>
  <mx:Label id="lblCurMousePos" text="Current Mouse Position"  color="#FFFFFF" fontSize="18" />
  <mx:Label id="lblCurMousePosCoord"
    text='{"(" + giMouseXLast + "," + giMouseYLast + ")"}'
    color="#FFFFFF" fontSize="18" />
 </mx:HBox>

</mx:Application>

No comments:

Post a Comment