Thursday, November 18, 2010

Drawing shapes using AS3: Part 3

Today we'll learn how to draw shapes with lines, fill them with a color and then remove the shape.

To fill a shape, it needs to have borders - lines. So, first of all, draw something that has an inner area. In my case, it is a triangle.

var myShape:Shape = new Shape();
addChild(myShape);

myShape.graphics.lineStyle(1, 0xFF0000, 1);
myShape.graphics.moveTo(50, 25); 
myShape.graphics.lineTo(120, 50);
myShape.graphics.lineTo(25, 80);

We can fill this using the beginFill(); method. Here is the syntax:

beginFill(hex_color, alpha);

It should be noted that beginFill should be called before we draw the lines. We also should use endFill(); method when we need to stop filling.

var myShape:Shape = new Shape();
addChild(myShape);

myShape.graphics.lineStyle(1, 0xFF0000, 1);
myShape.graphics.beginFill(0x990000,1); 
myShape.graphics.moveTo(50, 25); 
myShape.graphics.lineTo(120, 50);
myShape.graphics.lineTo(25, 80);
myShape.graphics.endFill(); 

And now you should have a red triangle.

Now, we might want to erase what we've drawn. Use the clear(); method for this.

myShape.graphics.clear();

And our drawing vanishes.

Thanks for reading!

Related:

Drawing shapes using AS3: Part 1
Drawing shapes using AS3: Part 2
Drawing shapes using AS3: Part 4
Drawing shapes using AS3: Part 5
Drawing shapes using AS3: Part 6
Drawing shapes using AS3: Part 7
Drawing shapes using AS3: Part 8

1 comment:

Anonymous said...

this blog rocks!

Post a Comment