Drawing Built-in Shapes

When you graduate from lines to shapes, you get to fill your shapes with a color. The lineStyle() method becomes optional, because shapes don't have to have an outline stroke. You can draw simple shapes using ActionScript's built-in methods. The technique is very similar to drawing lines and curves with the addition of the beginFill() method that lets you choose a color and transparency percentage for the fill color. Here's the step-by-step for drawing a rectangle and a circle in a MovieClip. You can find all the code in 18-4_Draw_Shape.fla in the Missing CD (www.missingmanuals.com/cds):

  1. Select File→New and choose ActionScript 3.0.

    A new, empty Flash document appears.

  2. Press F9 (Option-F9 on a Mac).

    The Actions panel opens, where you can enter ActionScript code.

  3. Type this line into the Actions panel to create an instance of the mcShapes MovieClip class:

    var mcShapes:MovieClip = new MovieClip();

    MovieClip is one of three data types that let you draw vector graphics. The other two classes are Sprite and Shape.

  4. Define a lineStyle() for your first shape:

    mcShapes.graphics.lineStyle(4,0x003300,.75);

    This statement uses the Graphics class, which is a property of the MovieClip class. As explained on Drawing Lines, the lineStyle() method can accept several parameters. This code uses three parameters, and the remaining ones are left unchanged from their original values. The first parameter sets the line or stroke thickness to 4 pixels. The second parameter provides a hexadecimal ...

Get Flash CS5: The Missing Manual now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.