A Flash Developer Resource Site

Results 1 to 4 of 4

Thread: Draw line to mouse and trace x,y every 5 pixels

  1. #1
    cake! Skribble_Style's Avatar
    Join Date
    Jun 2002
    Location
    Australia
    Posts
    550

    Draw line to mouse and trace x,y every 5 pixels

    Hello again. I'd like to draw a line from pointA to the mouse position, and trace along that line and return the x,y position every 5 pixels.

    If I could get any help on this it would be most appreciated.

    Thanks!


    Brendan
    Skribble

    Its not that im lazy, I just dont care.

  2. #2
    Pumpkin Carving 2008 ImprisonedPride's Avatar
    Join Date
    Apr 2006
    Location
    Grand Rapids MI
    Posts
    2,378
    Can you explain this a bit better? Maybe an illustration? Maybe an example of code you've tried? Help me help you. :/
    The 'Boose':
    ASUS Sabertooth P67 TUF
    Intel Core i7-2600K Quad-Core Sandy Bridge 3.4GHz Overclocked to 4.2GHz
    8GB G.Skill Ripjaws 1600 DDR3
    ASUS ENGTX550 TI DC/DI/1GD5 GeForce GTX 550 Ti (Fermi) 1GB 1GDDR5 (Overclocked to 1.1GHz)
    New addition: OCZ Vertex 240GB SATA III SSD
    WEI Score: 7.6

  3. #3
    Senior Member
    Join Date
    May 2010
    Location
    Russia: Western Siberia
    Posts
    268
    Well, it looks like you need to create something like a measuring tape, don't you?

    You'll have to use Point.distance() function to calculate the distance between 2 points.
    This method is basically a Pythagorean theorem.

    then use:
    graphics.lineStyle();
    graphics.lineTo();

    and

    graphics.clear(); to remove the line that was created a frame before

  4. #4
    ism BlinkOk's Avatar
    Join Date
    Aug 2001
    Location
    , location, location
    Posts
    5,002
    ok this is how you might do it in AS1. maybe some kind soul could translate it to AS2/3;
    1. create a blank mc and call it "lineMc" and place it on the stage
    2. lets assume pointA is an object containing the x/y point that you have
    3. code goes on the main timeline
    Code:
    //
    // Move the lineMc to the point
    lineMc._x = lineA.x;
    lineMc._y = lineA.y
    //
    // Calculate the angle to the mouse
    var d = { x: _xmouse - lineMc._x, y: _ymouse - lineMc._y };
    lineMc._rotation = ((Math.atan2(d.y, d.x)/Math.PI)*180);
    //
    // Get position of mouse relative to the lineMc
    var m = { x: _xmouse, y: _ymouse };
    lineMc.globalToLocal(m);
    // 
    // Now count every 5 pixels along the x-axis of the lineMc
    // until you are past the position of the mouse(m);
    for(var x=0; x < m.x; x += 5) {
       // x will now count off 5 pixel steps until it reaches (or exceeds)
       // the mouse position.
       // if you want the position relative to the main stage then you do this;
      var s = { y: 0, x: x };
      lineMc.localToGlobal(s);
       //
       // Now s should contains the (x,y) position of the current step
       // relative to the main stage
    }
    stop ();
    This is off the top of my head so it may not be 100% kosher syntax wise
    Last edited by BlinkOk; 01-02-2011 at 08:41 PM.
    Graphics Attract, Motion Engages, Gameplay Addicts
    XP Pro | P4 2.8Ghz | 2Gb | 80Gb,40Gb | 128Mb DDR ATI Radeon 9800 Pro

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center