Draw a line from any Point to any Point B dynamically using AS3

I'm doing an animation where there are several places (for example A, B, C, D,...) on a map. If a person clicks a location A and then B elsewhere, I wanted to draw a dotted line from point A (from the destination of the aircraft) to point B (to destination of the flat) using AS3. Please help me...

Here's a quick timeline and dirty code it does (class is below):

var line:Shape;
var lineGraphics:Graphics;
var clickSwitch:int = 0;
var mousePositions:Array = [];
var conversion:Number = 180 / Math.PI;

init();
function init():void {
     drawBackground();
     line = new Shape();
     addChild(line);
     lineGraphics = line.graphics;
     stage.addEventListener(MouseEvent.CLICK, onClick);
}

function onClick(e:MouseEvent):void
{
     clickSwitch = 1 - clickSwitch;
     mousePositions[clickSwitch] = new Point(mouseX, mouseY);
     if (clickSwitch == 0) {
          drawLine();
     }
     else {
          removeLine();
     }
}

function removeLine():void
{
     lineGraphics.clear();
     line.rotation = 0;
}

function drawLine():void
{
     lineGraphics.lineStyle(1, 0xff0000);
     lineGraphics.moveTo(0, 0);
     var dotDistance:Number = 2;
     var nextX:Number = dotDistance;
     while (line.width < Point.distance(mousePositions[0], mousePositions[1])) {
          lineGraphics.lineTo(nextX, 0);
          nextX += dotDistance;
          lineGraphics.moveTo(nextX, 0);
          nextX += dotDistance;
     }
     line.rotation =  conversion * Math.atan2(mousePositions[0].y - mousePositions[1].y, mousePositions[0].x - mousePositions[1].x);
     line.x = mousePositions[1].x;
     line.y = mousePositions[1].y;
}

function drawBackground():void
{
     var s:Shape = new Shape();
     var g:Graphics = s.graphics;
     g.beginFill(0xC0C0C0);
     g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
     g.endFill();
     addChild(s);
}

Class:

package
{
     import flash.display.Graphics;
     import flash.display.Shape;
     import flash.display.Sprite;
     import flash.events.MouseEvent;
     import flash.geom.Point;

     public class DottedLine extends Sprite
     {
          private var line:Shape;
          private var lineGraphics:Graphics;
          private var clickSwitch:int = 0;
          private var mousePositions:Array = [];
          private var conversion:Number = 180 / Math.PI;

          public function DottedLine()
          {
               init();
          }

          private function init():void {
               drawBackground();
               line = new Shape();
               addChild(line);
               lineGraphics = line.graphics;
               stage.addEventListener(MouseEvent.CLICK, onClick);
          }

          private function onClick(e:MouseEvent):void
          {
               clickSwitch = 1 - clickSwitch;
               mousePositions[clickSwitch] = new Point(mouseX, mouseY);
               if (clickSwitch == 0) drawLine();
               else removeLine();
          }

          private function removeLine():void
          {
               lineGraphics.clear();
               line.rotation = 0;
          }

          private function drawLine():void
          {
               lineGraphics.lineStyle(1, 0xff0000);
               lineGraphics.moveTo(0, 0);
               var dotDistance:Number = 2;
               var nextX:Number = dotDistance;
               while (line.width < Point.distance(mousePositions[0], mousePositions[1])) {
                    lineGraphics.lineTo(nextX, 0);
                    nextX += dotDistance;
                    lineGraphics.moveTo(nextX, 0);
                    nextX += dotDistance;
               }
               line.rotation =  conversion * Math.atan2(mousePositions[0].y - mousePositions[1].y, mousePositions[0].x - mousePositions[1].x);
               line.x = mousePositions[1].x;
               line.y = mousePositions[1].y;
          }

          private function drawBackground():void
          {
               var s:Shape = new Shape();
               var g:Graphics = s.graphics;
               g.beginFill(0xC0C0C0);
               g.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
               g.endFill();
               addChild(s);
          }

     }

}

Tags: Adobe Animate

Similar Questions

  • How to make the tool pen show a guide of my line from one point to another

    I need help on how to change my tool pen where it will show an overview of my lines from one point to another. When I draw the line I place two points and then the line appears. How I do to where the line will follow my mouse pointer and then I select the second anchor.

    Maybe check your preferences > selection & anchor display > enable the rubber band for: pen tool

  • I tried to copy several items which are separate from any application that I use.

    I'm updating (OS X v. 10.11.6) on all the improvements etc. use only the desktop iMac & use most of the time, & Contacts 'Pages', 'Safari '. In addition, I'm a 'ole' jerk who uses 1 finger of each hand type & am very slow; & I'm in a w/Chair with other disabilities, hearing, etc.

    N ° 1: I tried to copy several items which are separate from any application that I use. As the "second word through the fourth word, then somewhere further down in the same app anouther Word or group of words and so on."

    No. 2: I tried to connect you 'communities' and after several hours and a lot of frustration, I tried to get a new one, and that's how I found myself here!

    I would like an answer to #1, if you have any info that I can use. Otherwise, I'll try something new tomorrow.

    Thank you

    Bill C.

    < post branched out by host >

    Hello Warcloud7

    1. Double-click the first word of your desired channel
    2. SHIFT, click after the last word in the string to highlight the word two by four (see screenshot)
    3. COMMAND double click on another FIRST word "later on."
    4. SHIFT-click after the last word you want
    5. COPY however you are comfortable - Edit menu or command + C
    6. switch to some other app that you like
    7. Paste your favorite way

  • How to draw a line (with the line segment tool) then draw another line, from those previous anchor point. When I try immediately draw another line on the other anchor lines it try to turn instead.

    I'm following a course of basic fundamental illustrator on Lynda. The video of "draw straight lines" is troubling me. They draw a line with the line segment and then draw another line coming out the anchor point at an angle.

    Here's where it is troubling me - when I try to drag a line to the anchor point trying to transform the first line, I drew. (the line cursor turns into a selection tool) I can't draw another line connecting the first.

    Advice would be great because it's driving crazy me.

    See you soon,.
    Kyle

    Yes, it is irritating. You must clear the previous line first. If you want to make sure that both ends are perfectly aligned, start by turning on the guides. If you want two lines to be part of the same way, you should use the tool pen instead.

  • Draw a line connecting two points in the WPF chart

    I'm trying to understand the best way to draw a line between two existing points on my graph in WPF.  At first, I thought I had it works fine; I just added another parcel to my graph then given exactly two points for rendering.  But I quickly noticed that the app has become surprisingly slow and CPU was quite high (adding other plots does not affect the use of the CPU as long as they have a complete set of points (?)).  He wants me if I looked at using the DataToScreen method on the chart but I can't know what the parameters are.  The second parameter is simply an IList that tells me a lot.  This seems to be the method I want to use for the coordinates of a point on the graph screen.

    No one knows what the second parameter is supposed to be for the DataToScreen method?  Everything I tried throws an Exception of Argument.

    Thank you
    Dan

    DataToScreenwill return a screen coordinate in the plot area of a chart of the value of the raw data horizontal and vertical in the given list. The parameter value is a IList for compatibility with other methods of graphical query such as FindNearestValue , return IList values containing raw data.

    A concrete example, say you had a graphical configuration with a wide horizontal axis ranging from 0 to 100 and a vertical axis double precision ranging from 10 to 20:


            
                
                
            
        

    To get the screen coordinates of the value at the center of the two axes, you can call:

    graph.DataToScreen(plot, new object[] { 50, 15.0 })

    Note that the raw data values corresponding to the type of axes (horizontal AxisInt32 returns the integer 50 and vertical AxisDouble Gets the value 15.0 ).

    You also mentioned a performance issue where "adding other plots does not affect the CPU so that they have a complete set of points". I did a simple test app that adds an array of two points of a chart on each key (for example graph.Data.Add(new[] { new Point(x1, y1), new Point(x2, y2) }); ), but could not reproduce the problem you saw. Could you share the code, you use to draw the connection line?

  • Using custom control: draw a line and output start and end points

    I'm looking to find or make a custom control (or simple Subvi), which appears as a grid of 100 x 100 unit and allow me to draw a line from one point to another on this grid. It will then display (x, y) of departure and the point of this line on the grid.  Any help or ideas?

    Thank you

    Steve

    LabVIEW 2009 SP1

    You want essentially is a loop with an event structure when you process mouse down, move and up to events for the controls. There are several ways to implement something like that, but it will be probably the simplest:

    1. Use a table or a listbox multicolumn for your grid. Hide scrollbars and headers.
    2. You can use the ActiveCell property to-2, - 2 to select all cells. You can then use the cell size property to define the exact size of the cell.
    3. Then you put a picture on the top of the table control and color its transparent background so that the table shows through. Property node allows you to ensure that both line up on exactly the same location and size.
    4. You use the mouse on the image control events to detect clicks and moves.
    5. Point of the table to the method of the column line allows you to translate the position data of the event to a cell.
    6. You use the image control screws to draw the line on the photo based on these data.
    7. You can even color the cells selected in the table by using the table properties.

    If you want to simplify things a bit, you can also use the event of timeout instead of moving the mouse event to draw the line, but then you keep the timeout value in a shift register and hand it to-1 (no timeout) when the mouse event to the top.

    I would also say treatment mouse enter and leave events to change the cursor and cancellation if the user leaves in the middle of drag.

  • Problem by drawing a line dynamically

    Hello. I want to draw a line between two points in a dynamic way, it's average, by clicking on a canvas the first time will give the origin of the line and clicking will give once again, the end of the line (segment/stroke). I'll appreciate any help with this issue. Thanks in advance.

    Hello

    pls find the solution of your problem with the code below and let me know if you have any problem.

    Main.MXML

    http://www.Adobe.com/2006/mxml '.

    "Layout ="vertical"verticalAlign ="middle"xmlns:local =" * ">

    DrawingCanvas

    http://www.Adobe.com/2006/mxml"backgroundColor ="0xFFffff ".

    Width = "400" height = "300".

    creationComplete = "onCreationComplete () '"

    "xmlns:local =" * ">

    private function onCreationComplete():void

    {

    this.addEventListener (MouseEvent.MOUSE_DOWN, onMouseDown);

    this.addEventListener (MouseEvent.MOUSE_MOVE, onMouseMove);

    this.addEventListener (MouseEvent.MOUSE_UP, onMouseUp);

    }

    private var isMouseDown: Boolean;

    private var counter: number = 0;

    private var myShape: Shape = new Shape();

    private var : Point of origin;

    private var : destination Point;

    private function onMouseDown(event: MouseEvent):void

    {

    isMouseDown = true;

    origin = new Point (event.localX, event.localY);

    }

    private function onMouseMove(event: MouseEvent):void

    {

    if (isMouseDown is true)

    {

    destination = new Point (event.localX, event.localY);

    var lineThickness:Number = 2;

    var lineColor:Number = 0 x 000000;

    var lineAlpha:Number = 1;

    myShape.graphics.clear ();

    myShape.graphics.lineStyle (lineThickness, lineColor, lineAlpha);

    myShape.graphics.moveTo (origin.x, origin.y);

    myShape.graphics.lineTo (destination.x, destination.y);

    }

    }

    private function onMouseUp(event: MouseEvent):void

    {

    this.rawChildren.addChild (myShape);

    isMouseDown = false;

    }

    ]]>

    with respect,

    Mayeul Singh Bartwal

  • draw a line of nested MC to the other

    I use the drawing API to draw a line from one MC nested within two levels of MCs relative to another similarly nested MC. Can someone tell me how to get the X, Y coordinates of the two points?

    It's too much typing. If gp = grandparent, p = parent, c = child, use:

  • When I use the brush or eraser, an O symbol appears next to my cursor and prevents me from drawing a line. Does anyone know a solution to this problem?

    When I use the tool brush or eraser, over every other shot, I get a sign O (circle with a line through it) next to the brush shape / cursor and can't shoot. Please help, this is very frustrating and disrupting my work flow! I paint each frame of the animation, so you can imagine, every other shot is quite often.
    There the layers locked below my active drawing layer, that makes me wonder if Flash is trying to tell me I can not shoot these layers, but I know that more I need these layers there to guide the design of my active layer. I have encountered this problem before with layers being locked and these. What Miss me?

    Any idea is appreciated.

    Thank you!

    I just realized my problem!
    Sorry, it is partially due to my impatience; I get the "illegal sign" when I press a shortcut key to switch from one tool to the other and try to make a stroke when the tool has not finished "switching". So I'm going to press my button "e" with the intention to switch to Eraser tool and I will immediately draw a line, but recently, I noticed that the toolbar did not brush Eraser still at the point where I was drawing.
    So I'll check why my computer is so slow, or I'll try to be a little more patient, or both
    Thank you for your time!

  • I want to draw a line between 2 specific points on an image inserted in a Panel? Help, please!

    I am a beginner in working with LabWindows/CVI and I want to draw a line between 2 specific points on an image inserted in a Panel? Please help or examples of code.

    As evidenced by the Wolfgang there is a problem in the definition of recall. InstallCtrlCallback wants a reminder of the standard of review as a parameter: apparently you have structured your reminder callbackName on the prototype of a recall of Panel, which obviously missing the setting "control".

    In addition, the recall of control should be said somewhere before it can be used. In other words, you must add his statement before your function {main}, either in one of your include files, or directly in the code. This is the reason for the error 'found int, whereas CtrlCallbackPtr. '

    Third point: you create a new picture control in the callback assigned to the image control. And you reuse variable 'Test', which already holds a valid control ID...

    Finally: what you expect to make from '1' in callbackData parameter, given that you do not use anyway? Unless you want to adapt the behavior of recall some past in callbackData conditions, you must pass NULL as the parameter.

  • Cannot update Win7 HP64 or download from any microsoft Portal

    Help! I'm completely unable to update Win7 Home Premium 64, or downloading any content linked to or received by any portal Microsoft.

    Using Windows Update I got 80072EE2 error code. After searching all the forums I have found the only proposed solution was disable third-party firewalls and try again. I completely turned off all firewalls and antivirus, including Microsoft Defender software. It made no difference. I can't update Windows, Xbox Live, Windows Live or receive content from any of these. Also could not activate Win7 online.

    The only other thing I can think is that I am using a connection Mobile Broadband (Vodafone Vodem - Huawei NZ) running Vodafone Mobile Connect v9.2.1.6545) this could be a part of the problem? Never had any problems with XP.

    Can anyone help?

    SOLVED!

    Found this solution on... another forum. (sorry)

    Enter [netsh], then [winhttp] in the command line so that the screen looks like this later.

    Now enter the command [import source proxy = IE]. This will import the Internet Explorer proxy settings, which means either a direct access to the Internet without a proxy if no server is configured or the proxy server used to connect to the Internet.

    If the problem was a proxy server, this should solve problems connecting to Windows Update. WinHTTP may also be used to define a specific proxy server by using the [proxy set address] command. A reboot may be required before changes are taken into account (or kill the explorer.exe process and reload).

    Fortunately Windows updates as I type. Value, bit late, then 108 mb.

    Thanks for your help though!

  • How to customize my mozilla like gmail. (connection from any PC in the browser to get all my saved favorites)

    I NEED TO ACCESS MY BRPWSER ON DESKTOP FROM ANYWHERE IN THE WORLD... LIKE GMAIL...

    How to customize my mozilla like gmail. (connection from any PC in the browser to get all my saved favorites)

    (Personal information removed by the moderator. Please read the Forum rules and guidelines - m)

    Sorry, that Mozilla does not offer a feature like that.

    Check out Xmarks. http://www.Xmarks.com/about/features/overview

  • Cannot download from any site

    For some reason, Mozilla or don't let me download from any site, including your own. This was not always a problem. He just started a while back. Maybe after an update? I'm not sure.
    Hope you can help solve this problem.
    Thank you
    Nancy

    Then you may check the list of extensions and disable or remove those that are not essential or unrecognized. Open the page modules using either:

    • CTRL + SHIFT + a
    • Firefox orange (or the Tools menu) button > Add ons

    In the left column, click Extensions. Then, in case of doubt, disable or remove.

    Typically, a link will appear above at least an extension disabled to restart Firefox. You can complete your work on the tab and click one of the links in the last step.

    I hope that will completely get rid of it.

    If it comes back, you may have to uninstall something in the control panel.

  • can I access my email personalized from any computer?

    If I create a personalized email, can I access it from any computer?
    By the way, can I access Thunderbird from any computer?

    Thunderbird is installed o one computer. This is a request so unless your computer is turned on and you use something like logmein NO you can't access Thunderbird "any computer" is not all available data

  • HP Pavilion 500-210 QE cannot boot from any hard drive.

    Good evening. As you can see on the subject, my computer just cannot boot from any hard disk that I put on it. I tried swapping the power supply, SATA cable, reset the BIOS, allowing the start legacy, changing to AHCI to IDE and RAID...

    When I get to the BIOS Setup, I see the hard drive and its features, but it will not start or show when I am trying to install a new OS. I'm not an expert, but I think that the problem could be in the motherboard SATA ports, but I'm not sure. Just to be sure, I tried another harddrive and it did not work as well.

    I was also thinking to buy a HDD external and installation of my OS on it, but I don't even know if it is possible and I would really like to have my computer working again.

    Thank you for your attention!

    Ok. That doesn't say that it is very likely that the motherboard has problems.

    Contact HP for warranty repair.

Maybe you are looking for