flash as3计算二点之间的距离
[代码]
//画两小圆
var yuan1:Sprite=new Sprite();
yuan1.graphics.beginFill(0x000000);
yuan1.graphics.drawCircle(0,0,5);
addChild(yuan1);
yuan1.x=100;
yuan1.y=100;
var yuan2:Sprite=new Sprite();
yuan2.graphics.beginFill(0x000000);
yuan2.graphics.drawCircle(0,0,5);
addChild(yuan2);
yuan2.x=130;
yuan2.y=140;
//显赫距离的动态文本
var _txt:TextField=new TextField();
_txt.autoSize=TextFieldAutoSize.LEFT;
addChild(_txt);
//定义直线精灵
var zhixian:Sprite=new Sprite();
addChild(zhixian);
//圆1拖动和停止拖动
yuan1.buttonMode=true;
yuan1.addEventListener(MouseEvent.MOUSE_DOWN,yuan1ax);
function yuan1ax(event:MouseEvent):void {
yuan1.startDrag();
}
yuan1.addEventListener(MouseEvent.MOUSE_MOVE,yuan1yd);
function yuan1yd(event:MouseEvent):void {
event:gx;
}
yuan1.addEventListener(MouseEvent.MOUSE_UP,yuan1sk);
function yuan1sk(event:MouseEvent):void {
yuan1.stopDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP,yuan1yc);
function yuan1yc(event:MouseEvent):void {
yuan1.stopDrag();
}
//圆2拖动和停止拖动
yuan2.buttonMode=true;
yuan2.addEventListener(MouseEvent.MOUSE_DOWN,yuan2ax);
function yuan2ax(event:MouseEvent):void {
yuan2.startDrag();
}
yuan2.addEventListener(MouseEvent.MOUSE_MOVE,yuan2yd);
function yuan2yd(event:MouseEvent):void {
event:gx;
}
yuan2.addEventListener(MouseEvent.MOUSE_UP,yuan2sk);
function yuan2sk(event:MouseEvent):void {
yuan2.stopDrag();
}
stage.addEventListener(MouseEvent.MOUSE_UP,yuan2yc);
function yuan2yc(event:MouseEvent):void {
yuan2.stopDrag();
}
//更新
stage.addEventListener(Event.ENTER_FRAME,gx);
function gx(event:Event):void {
zhixian.graphics.clear();
var zuobiao1oint = new Point(yuan1.x, yuan1.y);
var zuobiao2oint = new Point(yuan2.x, yuan2.y);
var juli:Number = Point.distance(zuobiao1, zuobiao2);
zhixian.graphics.lineStyle(2,0xFF0000,1);
zhixian.graphics.moveTo(yuan1.x,yuan1.y);
zhixian.graphics.lineTo(yuan2.x,yuan2.y);
_txt.text="两点间距离:"+uint(juli*10)/10;
}
更多教程,请到 www.226500.cn/flash_a/flash.html
补充:flash教程,As3.0