代码之家  ›  专栏  ›  技术社区  ›  nabster023

拉伸和旋转电影剪辑而不失真

  •  1
  • nabster023  · 技术社区  · 6 年前

    我正在构建一个flash桌面应用程序,用户需要使用一条线路(或任何可以完成这项工作的东西)链接舞台上的两个Movieclip(一台计算机和一台路由器),我希望达到同样的效果: image1 。我搜索并找到了这个 solution ,我尝试了代码并做了一些修改:

    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所示相同的确切效果?

    2 回复  |  直到 6 年前
        1
  •  3
  •   Organis    6 年前
    // 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);
    }
    
        2
  •  2
  •   MESepehr    6 年前

    这是因为actionscript试图通过改变其容器MovieClip的比例来拉伸线条的厚度。但可以通过将“线比例”选项设置为“无”来防止出现这种情况。 The line Scale option

    为此,请选择线并打开“属性”菜单,然后从“缩放”选项的下拉菜单中选择“无”。

    但是 我建议您用代码画一条线: 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);