flash as3 代码画线问题,有a b两点,鼠标可拖动a点移动,过程中实时显示ab线段
var shape:Shape=new Shape();//定义画线容器
var temp_x:Number=0;//记录也鼠标的相对坐标
this.addChild(shape);
this.addEventListener(Event.ENTER_FRAME,DrawLineEvent);//帧频事件,用于画线
function DrawLineEvent(e:Event):void
{
shape.graphics.clear();//画线前先清空之前画的内容
shape.graphics.lineStyle(1, 0x000000);//定义线的宽度和颜色
shape.graphics.moveTo(a_mc.x,a_mc.y);
shape.graphics.lineTo(b_mc.x,b_mc.y);
shape.graphics.endFill();
}
a_mc.addEventListener(MouseEvent.MOUSE_DOWN,StartDragEvent);//添加鼠标拖动的功能
a_mc.addEventListener(MouseEvent.MOUSE_UP,StopDragEvent);
b_mc.addEventListener(MouseEvent.MOUSE_DOWN,StartDragEvent);
b_mc.addEventListener(MouseEvent.MOUSE_UP,StopDragEvent);
function StartDragEvent(e:MouseEvent):void
{
var obj:Object=e.currentTarget;
temp_x=this.mouseX-obj.x;
obj.addEventListener(Event.ENTER_FRAME,FollowMouseEvent);
}
function StopDragEvent(e:MouseEvent):void
{
var obj:Object=e.currentTarget;
obj.removeEventListener(Event.ENTER_FRAME,FollowMouseEvent);
}
function FollowMouseEvent(e:Event):void
{
var obj:Object=e.target;
obj.x=this.mouseX-temp_x;//通过相对坐标计算
}
好了,谢谢
不客气