Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Drawing lines with the mouse

Explorer ,
Oct 22, 2015 Oct 22, 2015

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!

TOPICS
ActionScript
314
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Community Expert , Oct 22, 2015 Oct 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++

}

Translate
Community Expert ,
Oct 22, 2015 Oct 22, 2015

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

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 22, 2015 Oct 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?

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2015 Oct 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++

}

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 22, 2015 Oct 22, 2015

Again, thank you so much!!!

I understand what you did exactly. But I doubt I would have come up with it.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 22, 2015 Oct 22, 2015
LATEST

you're welcome.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines