Friday, November 19, 2010

Drawing shapes using AS3: Part 4

Today we will learn how to draw rectangles using AS3.

Instead of drawing each side of a rectangle using lineTo, we can use the drawRect(); method. Here's the syntax:

drawRect(upperLeftCorner_X, upperLeftCorner_Y, width, height);

Here I draw a red rectangle:

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

myShape.graphics.lineStyle(1, 0xFF0000, 1);
myShape.graphics.beginFill(0x990000,1); 
myShape.graphics.drawRect(25, 25, 400, 200);
myShape.graphics.endFill(); 

I can also draw a rectangle with round corners using the drawRoundRect(); method. I'll need to specify the corner's width and height (its the same number):

drawRoundRect(upperLeftCorner_X, upperLeftCorner_Y, width, height, roundCornerDimensions);

For example:

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

myShape.graphics.lineStyle(1, 0xFF0000, 1);
myShape.graphics.beginFill(0x990000,1); 
myShape.graphics.drawRoundRect(25, 25, 400, 200, 40);
myShape.graphics.endFill(); 

The results:


Thanks for reading!

Related:

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

No comments:

Post a Comment