Flash gives you the great possibility to create graphics dynamically, that is, while your movie is running, via ActionScript. So, not everything has to be drawn manually. What are the benefits of this method of creating graphics? First of all, it reduces the filesize of your Flash movie. Second, it opens up an infinite number of possibilities for greater interactivity, creativity and more. And, best of all, it is very easy to learn. So, let’s start with the basics of drawing with ActionScript.
Drawing straight lines
1 Open a new Flash document. Call the first layer actions, ActionScript, or whatever you like. Click in the first (and only) keyframe of this layer and press F9 (PC) or ALT+F9 (Mac) to open up the Actions panel. Click in the right side of the panel and enter the following code:
_root.lineStyle(3, 0x0000FF, 100);
_root.lineTo(120, 75);
Test your movie (CTRL+ENTER for PC or Command+ENTER for Mac users). You should see a line appear in your Flash movie, looking exactly like the one on the image below:
Now, let’s explain the code we just entered. The first line,
_root.lineStyle(3, 0x0000FF, 100);
tells Flash to set a style for drawing. Every time you want to draw with ActionScript, the first thing you have to do is tell Flash how to draw. So, the command lineStyle
does exactly that, via the three parameters which follow between the parenthesis. The first one is line thickness. The value can be any number between 0 and 100, with 0 being the thinnest and 100 the thickest possible line. I chose the value 3 arbitrarily for this exercice. The next parameter tells Flash what colour should the line be. This is expressed by a hexadecimal value, with the characters 0x
preceding the value. The 0x
tells Flash that what follows is a hexadecimal value – if we omitted it, Flash would think it was some kind of variable. So, how can you know a specific colour’s hexadecimal code? Easy. Go to the Tools panel, to the Colors section, click a color (doesn’t matter if it’s line or fill color), hover with the mouse over the color of your choice and see its hexadecimal code (see image below).
So, you could say, that instead of writing the # before the color’s code, in ActionScript, we write 0x
. The last parameter defines the alpha property of the line – its transparency. This is a percentage value: if you enter 0, your line will be completely transparent – invisible on the stage. 100 means that the line is 100% opaque. You can even omit this parameter, like this:
_root.lineStyle(3, 0x0000FF);
If there is no alpha parameter at all, Flash uses the default value, which is 100. The _root
keyword at the beginning of the line means that you are setting the lineStyle
for the root timeline, the one you are working on right now (that means the Flash movie itself). You can use the drawing commands in ActionScripts either with root or with any movie clip (ex.: mymovieclip.lineStyle
).
The second line,
_root.lineTo(120, 75);
actually draws a line. The lineTo
command must always come after you have set the properties for drawing with the lineStyle
command. This time, the parameters between he parenthesis are coordinates (in pixels) on our stage. The first is x, the second is y. The zero-point of Flash’s coordinate system is situated in the top left corner of the stage. The positive x coordinates are to the right of it and the positive y coordinates are below it. See the image below for explanation.
So the piece of code lineTo(120, 75)
draws a line from (0, 0) to (120, 75). See this on the image below.
If you wanted to draw a horizontal line for example, you would add this line of code to our existing ActionScript (shown in bold):
_root.lineStyle(3, 0x0000FF, 100);
_root.lineTo(120, 75);
_root.lineTo(200, 75);
Test your movie. You will see a horizontal line starting exactly where the last one ended (120, 75) and with (200, 75) as finishing point.
But what if you wanted to start drawing somewhere else? And why did the first line started from (0,0) you may ask? Because this is the starting default point for drawing if you haven’t specified a different one. In order to start drawing from a point other than (0,0) or to continue drawing from another point, you must use the moveTo
ActionScript method. Add this to your code:
_root.lineStyle(3, 0x0000FF, 100);
_root.lineTo(120, 75);
_root.lineTo(200, 75);
_root.moveTo(300, 150);
_root.lineTo(180, 240);
You moved the beginning point of next line to (300, 150) and then you draw that line until the point (180, 240).
You can change the properties of your lines whenever you want. Add this code to the existing one and see it:
_root.lineStyle(3, 0x0000FF, 100);
_root.lineTo(120, 75);
_root.lineStyle(0, 0xC71D1F, 100);
_root.lineTo(200, 75);
_root.moveTo(300, 150);
_root.lineStyle(15, 0x25A988, 60);
_root.lineTo(180, 240);
Try drawing a square or a triangle. It is really easy. Now that you know how to draw straight lines, let’s see about curves.
Drawing curves
2 Save the previous document and open a new one. Call the layer actions, click into its first keyframe and open the Actions panel. Write in this code:
_root.lineStyle(0, 0x000000, 100);
_root.moveTo(100,100);
_root.curveTo(300, 100, 300, 300);
Test your movie, you should see a curve like the one in the image below appear.
As with straight lines, you must first define a style, which is what the first line of code serves for. The second line moves the drawing start position to (100, 100). I could have omitted that part, but I just didn’t want to start in (0, 0). You can choose to erase this line or enter any other value if you want to.
The third line draws a curve. Its generic syntax is as follows:
_root.curveTo(controlX, controlY, anchorX, anchorY);
where the last two parameters (anchorX
and anchorY
) are the coordinates of the finishing point of our curve. The first two parameters (controlX
and controlY
) are the coordinates of the point to which our curve flexes itself to. It acts like a “gravitational pull” point toward which the curve is being attracted to. I marked the control point and the imaginary pulling force on the image below so you can easier understand how this works. I also made the curve more thick so it can be seen easier.
Experiment with different control point coordinates to see how your curve behaves in response. You can also add a lineTo
command after that, Flash will just continue to draw with straight lines. After this, I recommend you see the tutorial about making fills with ActionScript.
but how would you make it so the viewer gets to draw a picture?
Very awesome. im new to action script but know other forms of programing. this is very helpful i will be reading all you stuff
milo: Well, Flash isn’t exactly Photoshop, but with AS 3.0 some amazing new things can be done. Stay tuned…
Jason: Subscribe to my RSS feed so you get instant notifications when a new tutorial is posted!
Sorry i am on the free trial and wanted to know a way you can use action script to create intertactive tabs over existing tabs that are static?
Any help would be most appreciated
Vicki
vicki: You can have static tabs in one layer and the meni in another.
very helpful and interesting study material
IS it possible to do a floorplan editor with flash??
and it should be online.
thanks
Hi,
Is it possible to zoom to the extent of your new drawing?
Tanks.
Umm, hi. I am kinda new to flash and am trying to not make pre-existing lines, as opposed to let my viewer able to draw the line after clicking on a button
For example:
Mouse Down – Start Drawing Line
Mouse Up – A perfectly straight/ Curved line to this point
Is this possible?
Thanks, Rand (Randy)
I loved these instructions. It was exactly what a student of mine needed to know to include in an etch-a-sketch type program.
thanks
This is nice tutorial
Thanks a lot.
This is nice tutorial
interesting
I have used this before, including beginFill. The question I have, that I haven’t been able to get an answer to, is can you make one drawing on top of the other? I have tried drawing on seperate layers, but they intersect rather than one overlapping the other. Thanks
well, how we delete the lines?
[…] X and Y coordinates equal zero). You can find more on this method in my tutorials that explains how to draw with ActionScript, since I don’t wish to repeat myself here. A nice figure will quickly make clear how the […]
Great basic tutorial on line drawing with AS3 (I need simple). Is it possible to draw the simple point to point lines adding point labels and generated from xml code so the lines can be drawn with the xml data?
Great basic tutorial, but how do you tween a line to a new x and y coordinate?
This is nice tutorial instructions.
You should also write something on drawing advanced geometry shapes as circle, rect, rounded rect, polygon etc.
And also add a tip to fill color in stroke.
Thanks,
Ravi Bhadauria
admec multimedia
very nice thx…………………
very good !
but how can i draw curve text?
please help me!!!!
This is really exciting… BTW if I want to make the line draggable and the end points will in fix position how can it be done…
If your going to be making a tut as at this simple level then it might be a good idea to list what sdk you are using, as well as show how you declared _root. I have gone through ten tuts already that have not done this very thing.
Awesome tutorial !!
Very good job .. Thanks for sharing 🙂