Using Actionscript to Draw Lines Dynamically
Using ActionScript, you can dynamically draw lines in a movie as it playsa capability that comes with a number of drawing methods available to the Movie Clip class.Using these drawing methods, you can:Draw a line from the current drawing position to a point you specifyMove the current drawing position without drawingSpecify the line style for a timelineFill a shape with a colorFill a shape with a gradientCurve a line between two pointsClear a timeline of drawn lines and gradients
In this section, we'll show you how to draw lines, move the drawing position, set the line style, and clear a movie clip instance. In the next section, we'll briefly touch on using flat and gradient fills. Although we won't cover the curveTo() method (which allows you to dynamically draw curved lines), you should understand enough about Flash drawing fundamentals by the end of the lesson that you'll be able to implement it in your own drawing applications.
Using lineStyle()
Before drawing any lines in a timeline, you must set the line style for that timeline. Flash uses this setting to determine line thickness, color, and alpha.Here's the syntax:
thickness must be a value between 0 and 255 (with a thickness of 0 representing a hairline). color must be a hex color value. alpha represents the transparency level for a line, where 0 is transparent and 100 is opaque. Look at the following example:
path.lineStyle(thickness, color, alpha)
This line of ActionScript sets the line style in myClip_mc so that all lines drawn will be green and opaque, and have a thickness of 10.
_root.myClip_mc.lineStyle(10, 0x009900, 100);
Using moveTo()
All movie clip instances have a drawing position that indicates the coordinate at which a line would startin other words, the beginning point of a line. (You use lineTo() to draw the line, as described in the next section.) When a movie clip instance is created, the drawing position is set as x = 0 and y = 0; however, you can move the drawing position at any time using moveTo(). When a line is drawn, the drawing position is updated to the endpoint of the drawn line.The following is the syntax for using moveTo():
All you need to do is specify the x and y positions of your drawing position. For example:
path.moveTo(x, y);
This ActionScript sets the line style and then moves the drawing position.
_root.myClip_mc.lineStyle(10,0x009900,100);
_root.myClip_mc.moveTo(100,100);

Using lineTo()
The lineTo() drawing method of the Movie Clip class simply draws a line of the destination timeline's lineStyle format from the drawing position to the end position specified in the method.Following is the syntax for lineTo():
The x and y parameters specify the end point of the line to be drawn.
myClip_mc.lineTo(x,y);
NOTEAfter the line is drawn, the moveTo() position is updated to the end point of the line.The following example shows how you would use the methods we've described to draw a line:
This ActionScript draws a line between the points (100,100) and (200,150).
_root.createEmptyMovieClip("canvas_mc",1);
_root.canvas_mc.lineStyle(2,0x009900,100);
_root.canvas_mc.moveTo(100,100);
_root.canvas_mc.lineTo(200,150);

• Table of ContentsMacromedia® Flash MX 2004 ActionScript: Training from the SourceBy
Derek Franklin, Jobe Makar Publisher: Peachpit PressPub Date: November 19, 2003ISBN: 0-321-21343-2Pages: 636
Sure, you can use Flash MX 2004 without being a master programmer, but as any Flash developer worth his or her salt will tell you, you''''re not tapping all of its power unless you''''re taking advantage of its scripting language "ActionScript 2.0" which offers a more robust programming model and better object-oriented programming support than ever before. Here to take the fear factor out of learning it are Flash veterans and best-selling authors Derek Franklin and Jobe Makar, who demonstrate that scripting is an instinctual process you already know by translating real-life activities into ActionScript scripts. In these pages, you''''ll find methodologies and techniques for building over 40 real-life Flash ActionScript projects, including sample games, applications, Web sites, and more. New in this edition are coverage of ActionScript 2.0, Web services, Components, Printing, Video, and more. On the companion CD, you''''ll find all the project files and images you need to complete each project.