For all the wonderful and neat things you can do with actionscript, it’s ironic that the simple task of drawing a line can be such a difficult thing. Flash has no native function to draw a line. Everything done in actionscript must be done within the context of movie clip. In this article I will explain how you can draw lines using movie clips.
First we must create a movie clip that will act as a line. Open a new FLA & make the size 200 by 200 pixels. Create a new movie clip in the library. Use the line tool(n) to make a horizontal line that starts as (0,0) and is 100 pixels wide.
Next, drag the line movie clip into the first frame. Right click on the movie clip and select Panels, Instance. Name this movie clip ‘line’. Now select the first frame from the timeline, and press F6 twice to create two new key frames. These are all the frames necessary to make our actionscript sequence.
Now let’s move on to the nitty gritty of the action script. Right click on the third keyframe and select Actions. Add this line to the action script:
This will ensure that when the movie is played, it will run through the actions in the first frame once, then repeatedly play the actions of frame two and three. This is important because we will put all of our initialization code and functions in the first frame. The main body of the actionscript will be placed in the second frame. The third frame will simple rewind back to frame two.
We’ll now make a function called ‘setline’ that will take four numbers (x,y,a,b) as parameters. This function will change the scale and rotation of the ‘line’ movie clip so that one end rests at (x,y) the other end at (a,b).
Now go to the Actions menu of the first frame. Put the following code into the first frame:
dist = Math.sqrt((x-a)*(x-a)+(y-b)*(y-b));
angle = (Math.atan((y-b)/(x-a))*(360/6.28));
if (avector.swf vector.fla
Robin Baker is a webmaster at WebproSchool.com. He also is the webmaster and owner of indirectdesign.com, a web and print design company on the forefront of design concepts.