RSS link icon

.

AS3 drawing part 2 lines draw


This is the second part of my Flash actionscript 3.0 tutorial on how to draw via the graphic api by coding.

In the previous tutorial I talked about how simple it is to draw a simple rectangle and change values to give it other properties like width, height and positioning.

In this Flash tutorial you will see how to draw straight lines defined by 2 points. Sounds simple? well it will be when your done with this tutorial.

actionscript tutorials

Again this is a programming tutorial like the previous, this means we do not need to do anything on the stage nor in the timeline, just go directly to the actionscript panel and type in the following code.

I have commented the code to explain what every single line does and how it works.

//First we declare our movieclip object, this is kind of a container for our drawing, 
//and we define how to refer to it (my_line).
var my_line:MovieClip = new MovieClip();
//here we attach our line movieclip to the main stage using the addChild
this.addChild(my_line);
//Now we give our line some properties, this is just to tell flash that we want our line thick, 
//we could use 1 as default, but I want people to notice it.
my_line.graphics.lineStyle(6);
//Here is another property, here we define the first starting point of the first line, (x and y axis)
my_line.graphics.moveTo(0,0);
//And the last thing is an ending point, when we define an ending point.
my_line.graphics.lineTo(35, 40);
//The previous end point will automatically be the new starting point for the next line, so therefore we can build more lines on it, as I did.
my_line.graphics.lineTo(60, 60);
my_line.graphics.lineTo(80, 100);
my_line.graphics.lineTo(60, 160);

Soulman says: 2008-09-16

TypeError: Error #1034: Type Coercion failed: cannot convert flash.display::MovieClip@13bae311 to mx.core.IUIComponent. at mx.core::Container/http://www.adobe.com/2006/flex/mx/internal::addingChild() at mx.core::Container/addChildAt() at mx.core::Container/addChild() at flashUpdateTest/drawLine() at flashUpdateTest/onCreationComplete() at flashUpdateTest/___flashUpdateTest_Application1_creationComplete() at flash.events::EventDispatcher/dispatchEventFunction() at flash.events::EventDispatcher/dispatchEvent() at mx.core::UIComponent/dispatchEvent() at mx.core::UIComponent/set initialized() at mx.managers::LayoutManager/doPhasedInstantiation() at Function/http://adobe.com/AS3/2006/builtin::apply() at mx.core::UIComponent/callLaterDispatcher2() at mx.core::UIComponent/callLaterDispatcher()

Admin Bob says: 2008-09-22

Hi Soulman, sorry my comments fields is having trouble displaying code correctly, but you can email me the source code and I will have a look at your problem

Admin Bob says: 2008-03-29

hi chris, well you are right, if you are doing complex games or something like that, a sprite would be great, but for small examples as this, the resources does not matter much. But yes I love the sprite container, its very useful.

chris says: 2008-03-28

The MovieClip class uses a massive amount of resources. I think the sprite class would be more appropriate.

xavier says: 2008-03-10

first