Skip to main content
Known Participant
July 28, 2011
Question

Line Drawing Script on Stage doesn't work in MovieClip?

  • July 28, 2011
  • 1 reply
  • 1141 views

I have a little snippet of code that allows the user to create a line with the mouse, and clears the line if they end up touching the hitbox with the mouse, while they're drawing the line. It works fine when it's just thrown onto the main timeline. However, when I try to encapsulate it within a movie clip, it suddenly doesn't work. At all. Are there any suggestions as to why this is happening? Here's the code.

var drawing:Boolean;
var my_line:MovieClip = new MovieClip();
this.addChild(my_line);

drawing = false;//to start with
stage.addEventListener(MouseEvent.MOUSE_DOWN, startDrawing);
stage.addEventListener(MouseEvent.MOUSE_MOVE, draw);
stage.addEventListener(MouseEvent.MOUSE_UP, stopDrawing);

function startDrawing(event:MouseEvent):void
{
//move to the correct starting position for drawing
my_line.graphics.lineStyle(3,0xFF0000);
my_line.graphics.moveTo( mouseX, mouseY);
if (drawBox.hitTestPoint(mouseX,mouseY,true))
{
  drawing = true;
}

}

function draw(event:MouseEvent)
{
if (drawing)
{
  my_line.graphics.lineTo(mouseX,mouseY);
  if (oneHitBox.hitTestPoint(mouseX,mouseY,true))
  {
   my_line.graphics.clear();//remove line
  
  }
}
}

function stopDrawing(event:MouseEvent)
{
drawing = false;
}

Thanks for any help you can offer!

This topic has been closed for replies.

1 reply

kglad
Community Expert
Community Expert
July 29, 2011

put the movieclip that contains that code at 0,0 and retest.

RDelfanAuthor
Known Participant
July 29, 2011

That indeed seems to cause the code to work, though I can only see about a quarter of the movie clip, due to where it's placed. Why does it work that way, and what can be done to make sure I can see all of the movie clip AND be able to draw the line?

kglad
Community Expert
Community Expert
July 29, 2011

the location of your drawing is relative to the position of my_line.  if my_line is in the center of the stage on the main timeline, that's where your drawing will appear.  but if my_line is nested in movieclips, the position of your lines will be dependent on all its parents registration points.

you can use the displayobject's globaltolocal() method to control where your line appears.

p.s.  please mark correct/helpful responses, if there are any.