Drawing shapes with AS3
by admin on May.30, 2009, under Tutorials
Drawing any shapes with AS3 is very easy. In this tutorial I will show you how to draw a basic shape and then how to apply this knowledge into a dynamic application.
1. Open your Flash CS3 software, select “new”, “Flash File (Actionscript 3) “, click “Ok”, then go to “Modify”, “Document”, enter 300 for the width and height and enter 30 for the frame rate.
2. Open the “Actions” panel.
3. Copy and Paste this code:
var square:Sprite = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x = stage.stageWidth/2-square.width/2;
square.y = stage.stageHeight/2-square.height/2;
4. Hit “Ctrl+Enter” or go to “Control”, “Test Movie” to test our Flash movie. You should see this:
Here is what the code does:
var square:Sprite = new Sprite();
We create a sprite object which is pretty much like a MovieClip but without a timeline.
addChild(square);
We add our sprite to the display list which means we tell Flash that we want this sprite to be displayed.
square.graphics.lineStyle(3,0x00ff00);
We assign a graphics object to our sprite (If you worked with Java that might seem pretty familiar). Then we assign a line style to the graphic object.
square.graphics.beginFill(0x0000FF);
We assign a fill color to our graphic object.
square.graphics.drawRect(0,0,100,100);
We draw a rectangle using the drawRect() method.
square.graphics.endFill();
We tell Flash we are done drawing. (the other two lines of code are for positioning).
Now since there are many platforms and many ways to write AS3 code, I’m going to try to make this tutorial as versatile as possible so here is a document class version that you can use with a Flex Actionscript project or simply by using the document class box in the Flash CS3 environment. So copy and paste the following code in Notepad for example and save it as simple_shape.as:
package{
import flash.display.Sprite;
public class simple_shape extends Sprite{
var square:Sprite;
public function simple_shape(){
square = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRect(0,0,100,100);
square.graphics.endFill();
square.x = stage.stageWidth/2-square.width/2;
square.y = stage.stageHeight/2-square.height/2;
}}
}
Now in Flex, set this file as the document class, and in Flash CS3 write “simple_shape” in the document class box. Run the example.
Now starting from a document class, it’s easy enough to create a real class out of it so I’ll leave that up to you.
We can as easily create a rounded rectangle:
var square:Sprite = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRoundRect(0,0,100,100,20);
square.graphics.endFill();
square.x = stage.stageWidth/2-square.width/2;
square.y = stage.stageHeight/2-square.height/2;
Document Class (simple_rounded_shape.as):
package{
import flash.display.Sprite;
public class simple_rounded_shape extends Sprite{
var square:Sprite;
public function simple_rounded_shape(){
square = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
square.graphics.drawRoundRect(0,0,100,100,20);
square.graphics.endFill();
square.x = stage.stageWidth/2-square.width/2;
square.y = stage.stageHeight/2-square.height/2;
}}
}
And here is the result:
You can of course get a total control on how the shape is drawn with the good old moveTo() and lineTo() methods:
var square:Sprite = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
drawShape(square);
square.graphics.endFill();
square.x = stage.stageWidth/2-square.width/2;
square.y = stage.stageHeight/2-square.height/2;
function drawShape(sprite:Sprite):void{
sprite.graphics.lineStyle(3,0x00ff00);
sprite.graphics.beginFill(0x0000FF);
sprite.graphics.moveTo(0,0);
sprite.graphics.lineTo(100,0);
sprite.graphics.lineTo(100,100);
sprite.graphics.lineTo(0,100);
sprite.graphics.lineTo(0,0);
sprite.graphics.endFill();
}
Document Class (simple_lineTo_shape.as):
package{
import flash.display.Sprite;
public class simple_lineTo_shape extends Sprite{
var square:Sprite;
public function simple_lineTo_shape(){
square = new Sprite();
addChild(square);
square.graphics.lineStyle(3,0x00ff00);
square.graphics.beginFill(0x0000FF);
drawShape(square);
square.graphics.endFill();
square.x = stage.stageWidth/2-square.width/2;
square.y = stage.stageHeight/2-square.height/2;
}
private function drawShape(sprite:Sprite):void{
sprite.graphics.lineStyle(3,0x00ff00);
sprite.graphics.beginFill(0x0000FF);
sprite.graphics.moveTo(0,0);
sprite.graphics.lineTo(100,0);
sprite.graphics.lineTo(100,100);
sprite.graphics.lineTo(0,100);
sprite.graphics.lineTo(0,0);
sprite.graphics.endFill();
}}
}
MoveTo() and lineTo() methods were already part of AS2 and I covered how to use them in details in this AS2 tutorial. These are actually the methods that we will need most of the time if we want to create a dynamic shape. Let’s see that in the next page.
January 28th, 2010 on 5:43 pm
Donner des noms aux objects que l’on genere est purement facultatif. Cela demande un peu d’experience d’arriver a travailler avec des objects sans noms mais une fois que l’on connait la technique cela n’est vraiment pas difficile. Je pense qu’il y a une class version dont on peut se sevir comme document class. Celle ci devrait marcher.
January 28th, 2010 on 10:32 am
Intéressant merci.
Et cmment ferait-on pour créer plusieurs de ces carrés et les manipuler comme des MC? Par exemple créér noms dynamiques: carre1, carre2, carre3… ?
Autre question/pb: le code sous forme de .as sous Flash CS4 ne marche pas, mais est sans erreur; je ne comprends pas pour quoi il n’affiche rien (objet créé mais pas sur la scène).
December 14th, 2009 on 8:37 pm
Can you be more precise? Which part exactly?
December 14th, 2009 on 1:45 pm
Nice post, thanks! Could you tell me about the third paragraph in more detail?
September 17th, 2009 on 2:50 pm
Yes it’s possible if you use Flash CS4. You can also check many of the open source 3D API like Papervision, away3D and such.
September 17th, 2009 on 5:42 am
is it possible to make 3D shapes?
August 4th, 2009 on 10:57 pm
I’ll try I promise. I’m just a bit busy right now writing tutorials on other subjects and of course, making a living! (or trying). But anyways, yes I will write more about drawing with AS3 this year.
August 4th, 2009 on 10:44 pm
I love your tuts, thanks.
Can you make some more tutorial about drawing like curve, circle, elip…
I have just search around world but no have any tutorial usefull like this
Thanks alots
Aries
July 12th, 2009 on 5:16 pm
When creating sin waves it’s important to keep each bezier curve points in relation to each other so the drawn curve does not brake. Then the amplitude of such waves can be controlled by giving a max and min value to the target bezier curve points. If some readers are interested I’ll be happy to write a quick tutorial about this practice.
July 9th, 2009 on 10:39 am
thanks. how would you create random sin wave/abstract shapes. like the abstract backgrounds of adobe.com
thanks