Mouse distance with pythagorean theorem
/**
* Copyright terravilor ( http://wonderfl.net/user/terravilor )
* MIT License ( http://www.opensource.org/licenses/mit-license.php )
* Downloaded from: http://wonderfl.net/c/d9Kw
*/
package {
import flash.text.TextField;
import flash.text.TextFieldAutoSize;
import flash.display.Sprite;
import flash.events.Event;
public class MouseDistance extends Sprite {
private var sprSquare:Sprite;
private var txtDebug:TextField;
public function MouseDistance() {
init();
}
public function init():void {
sprSquare = new Sprite;
sprSquare.graphics.beginFill(0xFF0000);
sprSquare.graphics.drawRect(-2, -2, 4, 4);
sprSquare.graphics.endFill();
sprSquare.x = stage.stageWidth / 2;
sprSquare.y = stage.stageHeight / 2;
addChild(sprSquare);
//debug
txtDebug = new TextField();
txtDebug.autoSize = TextFieldAutoSize.LEFT;
addChild(txtDebug);
addEventListener(Event.ENTER_FRAME, onMouseMove); //usando esse evento por causa do wonderfl
//addEventListener(MouseEvent.MOUSE_MOVE, onMouseMove);
}
protected function onMouseMove(event:Event):void {
txtDebug.text = "oi";
graphics.clear();
graphics.lineStyle(1, 0, 1);
graphics.moveTo(sprSquare.x, sprSquare.y);
graphics.lineTo(mouseX, mouseY);
var dY:Number = sprSquare.y - mouseY;
var dX:Number = sprSquare.x - mouseX;
var dist:Number = Math.sqrt(dX * dX + dY * dY);
txtDebug.text = dist.toString();
}
}
}