O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Drawing Shapes

To draw an arbitrary shape (i.e., to paint a color into the geometric area between three or more points), follow these steps:

  1. Choose the starting point of the shape (either the default (0,0) or a point specified via moveTo( )).

  2. Start the shape with the beginBitmapFill( ), beginFill( ), or beginGradientFill( ) method.

  3. Draw the shape's outline with a series of lineTo( ) and/or curveTo( ) calls, the last of which should end at the starting point specified in Step 1.

  4. Close the shape with endFill( ).

The beginFill( ) method fills the shape with a solid color; the beginGradientFill( ) method fills the shape with a gradient (a blend between two or more colors); and the beginBitmapFill( ) method fills a shape with the specified bitmap (tiled if desired).

For example, the following code draws a red triangle with a five pixel-thick black outline. Notice that the default start point (0, 0) matches the endpoint:

var triangle:Shape = new Shape();
triangle.graphics.beginFill(0xFF0000, 1);
triangle.graphics.lineStyle(20);
triangle.graphics.lineTo(125, 125); // Draw a line down and right
triangle.graphics.lineTo(250, 0);   // Draw a line up and right
triangle.graphics.lineTo(0, 0);     // Draw a line left
triangle.graphics.endFill();
addChild(triangle);

Figure 25-4 shows the result of the preceding code.

A triangle

Figure 25-4. A triangle

Notice that the corners of the triangle in Figure 25-4 are rounded. To ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required