Skip to main content
Participating Frequently
October 22, 2015
Answered

Drawing lines with the mouse

  • October 22, 2015
  • 1 reply
  • 364 views

I've been scratching my head on this all morning. I've added rectangles to the stage at runtime via AS 3.0. I drag those rectangles elsewhere on the stage. Now I'd like to use some method to click on one of those rectangles and draw a line to anywhere else on the stage - ideally another rectangle. I thought about using RIGHT_MOUSE_UP and RIGHT_MOUSE_DOWN but those don't seem to work on the Mac using the magic mouse. It's treated as a left click. Anyone have any sample code that might do what I'm looking for?

Thanks!

This topic has been closed for replies.
Correct answer kglad

sure:

var clickNum: int = 0;

stage.addEventListener(MouseEvent.CLICK, clickF);

function clickF(e: MouseEvent): void {

    with(this.graphics) {

        lineStyle(1, 0xff0000);

        if(clickNum % 2 == 0) {

            moveTo(this.mouseX, this.mouseY);

        } else {

            lineTo(this.mouseX, this.mouseY);

        }

    }

    clickNum++

}

1 reply

kglad
Community Expert
Community Expert
October 22, 2015

are you asking about the graphics moveTo and lineTo or asking about right mouse events?

Participating Frequently
October 22, 2015

LineTo. For example, I move my mouse to a point on the canvas and click. Then I move the mouse to another point on the canvas and click and a line is drawn between the two points I clicked.

OR

Is it possible to dynamically draw a line between two objects on the canvas?

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
October 22, 2015

sure:

var clickNum: int = 0;

stage.addEventListener(MouseEvent.CLICK, clickF);

function clickF(e: MouseEvent): void {

    with(this.graphics) {

        lineStyle(1, 0xff0000);

        if(clickNum % 2 == 0) {

            moveTo(this.mouseX, this.mouseY);

        } else {

            lineTo(this.mouseX, this.mouseY);

        }

    }

    clickNum++

}