Drawing a line over 12 frames

[FONT=Arial]Hi all,[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial]I was looking for tutorials on drawing in flash using actionscript and all of the tutorials I found showed how to create content instantly (with the click of a button etc).[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial]But I was wondering if it was possible to use actionscript to draw a line from coordinate A to B over, say 12 frames?[/FONT]
[FONT=Arial] [/FONT]
[FONT=Arial]Can anyone help me out?[/FONT]

Sure =)

function drawLine(x1:Number, y1:Number, x2:Number, y2:Number, duration:Number, target:MovieClip) {
	var counter:Number = 1;
	var width:Number = x2-x1;
	var height:Number = y2-y1;
	var curX:Number = x1;
	var curY:Number = y1;
	var stepX:Number = width/duration;
	var stepY:Number = height/duration;
	target.lineStyle(1, 0x00ff00, 100);
	target.moveTo(curX, curY);
	curX += stepX;
	curY += stepY;
	this.onEnterFrame = function() {
		target.lineTo(curX, curY);
		curX += stepX;
		curY += stepY;
		if (counter == duration) {
			delete this.onEnterFrame;
		}
		counter++;
	};
}
// draws line in current MC
// from 50,50 to 100,200 during 30 frames
drawLine(50, 50, 100, 200, 30, this);

awsume!

thats exactly what i needed, thanks mathew.er!

C:-)

how could i modifiy that so that i could draw from frame 12 to frame 24?

thanks again for help

Call it at frame 12. :wink:

lol, didnt even cross my mind to do that, thanks again :thumb:

LOL :m2:

[FONT=Arial]i’m trying to use the function to draw two lines at the same time by calling it in different layers but the same frame, however i’m getting some funny results and can’t figure out why?

[COLOR=black]also i can’t make it draw a line after the other has finished e.g. call it in layer 1 frame 1 with a duration of 12 and then in layer 2 frame 13, [/COLOR]
[COLOR=black]ne ideas?[/COLOR][/FONT]

I reworked that function and now it is a MC’s prototype… that means that you can call it for any Movieclip instance as McInstanceName.drawLine(parameters). First 4 parameters are start and end point coordinates, then its duration in frames, color (in hex i.e. 0xccff6e), thickness of the line and its opacity from 0 to 100.

MovieClip.prototype.drawLine = function(x1:Number, y1:Number, x2:Number, y2:Number, duration:Number, color:Number, thickness:Number, alpha:Number):Void  {
	var counter:Number = 1;
	var lineWidth:Number = x2-x1;
	var lineHeight:Number = y2-y1;
	var curX:Number = x1;
	var curY:Number = y1;
	var stepX:Number = lineWidth/duration;
	var stepY:Number = lineHeight/duration;
	this.lineStyle(thickness, color, alpha);
	this.moveTo(curX, curY);
	curX += stepX;
	curY += stepY;
	this.onEnterFrame = function() {
		this.lineTo(curX, curY);
		curX += stepX;
		curY += stepY;
		if (counter == duration) {
			delete this.onEnterFrame;
		}
		counter++;
	};
};

note: It is not possible to draw two lines at time into one MC. If you try to do so, it will stop drawing one and start drawing the other line.

[COLOR=black][FONT=Arial]absolute legend,[/FONT][/COLOR]

[COLOR=black][FONT=Arial]thankyou so much and sorry to have taken up so much of your time. [/FONT][/COLOR]

I’m allways glad to help =)