- Home
- Tutorials
- Flash
- Intermediate
- Dynamic Drawing Using ActionScript
Dynamic Drawing Using ActionScript

page1
Josh Buhler
This user is yet to take control of their account and provide a biography. If you are the author of this article, please contact us via support AT actionscript DOT org.
View all articles by Josh BuhlerWhen Flash MX was introduced, several new features were added to both the authoring enviroment and to ActionScript. My favorite group of new methods added were the drawing methods added to the MovieClip class, also referred to as the Drawing API. These methods allow you to dynamically draw shapes on the stage of the Flash Player at runtime. Now, you may be thinking one of a few things now: "Neat. I can draw a square with ActionScript. Wow." or, "when would I ever use this?", or if you're like me, "SWEET! Now what can I do with this?" If you were thinking one of the first two things I just mentioned, keep reading, and I'll show you what you can do with this, and why you would want to use it. If you're excited about this and what to see just what you can do, I'll show you what I've already done with it. There are some pretty basic concepts here, but you'll be able to do some amazing things once you get them down.
First, some basics - The Drawing API consists of eight methods:
lineStyle()
moveTo()
lineTo()
curveTo()
beginFill()
beginGradientFill()
endFill()
clear()
Each of these methods allows you to dynamically draw a shape on the stage using a MovieClip. The easiest way to visualize how this thing works is to imagine yourself at a desk with a pen and a piece of paper. The first thing you'll do is pick what pen you want to use. You'll then decide where you want to start drawing at, and move your pen to that spot. You then decide where you want your lines to end, and draw them to that point. If you want to start a new line, you'll move your pen to that spot, and start again.
The Drawing API works in a similar method. First, you already have your paper, the stage. To draw, you'll also need a "pen". This is a MovieClip. You can use a MovieClip already placed on the stage, or, you can create one dynamically using createEmptyMovieClip. You then need to decide what type of "pen" you want. This is done by using the lineStyle() method. lineStyle() allows you to specify the line thickness, color, and alpha value of the line. You then move the "pen" to the position you'd like to start drawing from using moveTo(), and then draw your line using either lineTo() or curveTo(). If you would like to fill this shape, you would use either beginFill() or beginGradientFill(), combined with endFill(). To wipe the slate clean and erase your drawings, the clear() method is called.
But enough talk, on to the coding -
First, a quick example of this thing in action. The code below draws a simple black box on the stage, with no fill, followed by a solid black box, with a red border. To view this in action, open a new .FLA, and paste the code below into the first frame.
Download the source files for this tutorial - drawingAPI.zip
example1.fla
// first create a 'pen' to draw with
createEmptyMovieClip("pen_mc",5);
//
// set the line style - 0 pixels thick, black, 100% alpha
pen_mc.lineStyle(0,0x000000,100);
//
// move the pen to 100 x,100 y on the stage
pen_mc.moveTo(100,100);
//
// draw the next lines, each starting from the last point the pen was at
pen_mc.lineTo(200,100);
pen_mc.lineTo(200,200);
pen_mc.lineTo(100,200);
pen_mc.lineTo(100,100);
//
// start the next box
// set the new line style - 5 pixels thick, red, 100% alpha
pen_mc.lineStyle(5,0xff0000,100);
//
// move the pen to 400 x,100 y on the stage
pen_mc.moveTo(300,100);
//
// start the fill
pen_mc.beginFill(0x000000,100);
// draw the next lines, each starting from the last point the pen was at
pen_mc.lineTo(400,100);
pen_mc.lineTo(400,200);
pen_mc.lineTo(300,200);
pen_mc.lineTo(300,100);
//
// end the fill
pen_mc.endFill();
The code above produces the following:


