拉伸和旋转 Movieclip 而不变形

Stretch and rotate a Movieclip without distortion

我正在构建一个 Flash 桌面应用程序,用户需要在其中 link 使用一条线(或任何可以完成这项工作的任何东西)在舞台上(一台计算机和一个路由器)两个 Movieclips,我想实现同样的效果:image1. I searched and found this ,我尝试了代码并做了一些修改:

link.addEventListener(MouseEvent.CLICK, linkOnClick);
function linkOnClick(e:MouseEvent){

this.addEventListener(Event.ENTER_FRAME, enterFrame);

var linkPoint:Point = new Point(link.x, link.y);
var mousePoint:Point = new Point();
var distance:Number;
var radians:Number;

function enterFrame(e:Event):void { 

    //Distance
    mousePoint.x = stage.mouseX;
    mousePoint.y = stage.mouseY;
    distance = Point.distance(linkPoint, mousePoint);
    link.width = distance;

    //Rotation
    radians = Math.atan2(stage.mouseY - link.y, stage.mouseX - link.x);
    link.rotation = radians * (180/ Math.PI);

    if(link.hitTestObject(router)){trace("Success");}
}

当我编译代码时,我得到了这个:image2,所以你可能会说,我发现的问题是:

1-线条的边缘跟随鼠标的方向,但有时会超出光标,我想让光标拖动线条的边缘。

2-线条改变它的宽度,如果是90度,线条宽度非常显着,我希望线条具有恒定的宽度。

我怎样才能达到与图片 1 中显示的完全相同的效果?

这是因为动作脚本试图通过更改其容器 MovieClip 的比例来拉伸线条粗细。但是您可以通过将线比例选项设置为 None 来防止这种情况发生。

为此,select 您的行并打开属性菜单,然后从“缩放”选项的下拉菜单中 select None。

但是, 我建议你通过代码划线:Draw line from object to Mouse (AS3)

写下代码:

this.graphic.clear ();
this.graphic.lineStyle(0x000000);
this.moveTo(startPoint.x,startPoint.y);
this.lineTo(endpoint.X,endpoint.y);
// First, lets create mouse-transparent container for drawing.
var DrawingLayer:Shape = new Shape;
addChild(DrawingLayer);

// Hook the event for starting.
stage.addEventListener(MouseEvent.MOUSE_DOWN, onDown);

// Define a storage for keeping the initial coordinates.
var mouseOrigin:Point = new Point;

function onDown(e:MouseEvent):void
{
    // Save the initial coordinates.
    mouseOrigin.x = DrawingLayer.mouseX;
    mouseOrigin.y = DrawingLayer.mouseY;

    // Hook the events for drawing and finishing.
    stage.addEventListener(MouseEvent.MOUSE_UP, onUp);
    stage.addEventListener(MouseEvent.MOUSE_MOVE, onDraw);
}

function onDraw(e:MouseEvent):void
{
    // Remove the previous line.
    DrawingLayer.graphics.clear();

    // Draw a new line.
    DrawingLayer.graphics.lineStyle(5, 0xFF6600);
    DrawingLayer.graphics.moveTo(mouseOrigin.x, mouseOrigin.y);
    DrawingLayer.graphics.lineTo(DrawingLayer.mouseX, DrawingLayer.mouseY);
}

function onUp(e:MouseEvent):void
{
    // Unhook the events for drawing and finishing.
    stage.removeEventListener(MouseEvent.MOUSE_UP, onUp);
    stage.removeEventListener(MouseEvent.MOUSE_MOVE, onDraw);
}