PDA

View Full Version : Draw a moving line from (400,100) to (500, 150)



florencec2046
August 25th, 2008, 03:31 AM
Hi hi,

I am newbie in AS3. This forum is great. With the replies of my previous post, I have learned how to draw vertical, horizontal or with 45 degrees moving line.

But what if I want to draw a line from (400,100) to (500, 150).... I think I need to use some sine, cos or tan formula to increment ... right ?? :

-----------------------------------------------------------

var line:Sprite = new Sprite;
addChild(line);

with (line.graphics) {
clear();
moveTo(400, 100);
lineStyle(1, 0xff6600);
lineTo(500, 150);
}

var sp:Sprite = new Sprite;
addChild(sp);
var lineY:uint = 100;
var lineX:uint = 400;

function drawLine(e:Event):void {

with (sp.graphics) {
clear();
moveTo(400, 100);
lineStyle(1, 0xff6600);
lineTo(lineX++, lineY++); // I know this is not correct !!!!
}
trace("lineX = ", lineX, " lineY = ", lineY);

if ((lineY>150) || (lineX>500)) {
removeEventListener(Event.ENTER_FRAME,drawLine);
}
}

addEventListener(Event.ENTER_FRAME,drawLine);

------------------------------------------------------------------------

Please show me how to achieve this.

Thanks

Florence

TheCanadian
August 25th, 2008, 04:31 AM
var currPoint:Point = new Point(400, 100);
var endPoint:Point = new Point(500, 150);
var diffPoint:Point = endPoint.subtract(currPoint);
var angle:Number = Math.atan2(diffPoint.y, diffPoint.x);
var speed:Number = 3;
var dx:Number = Math.cos(angle) * speed;
var dy:Number = Math.sin(angle) * speed;

var line:Sprite = new Sprite();
addChild(line);
with (line.graphics) {
lineStyle(1, 0xff6600);
moveTo(currPoint.x, currPoint.y);
lineTo(endPoint.x, endPoint.y);
}

var sp:Sprite = new Sprite();
addChild(sp);
with(sp.graphics) {
lineStyle(3, 0xff6600);
moveTo(currPoint.x, currPoint.y);
}

function drawLine(e:Event):void {
sp.graphics.lineTo(currPoint.x += dx, currPoint.y += dy);
if (currPoint.y > 150) {
removeEventListener(Event.ENTER_FRAME, drawLine);
}
}

addEventListener(Event.ENTER_FRAME, drawLine);

florencec2046
August 25th, 2008, 05:33 AM
Thank you so much. :}

Florence