How Do I Draw with Code?: Chapter 12 - ActionScript 3.0 Quick Reference Guide
Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

12.6 Defining a Fill Style

Problem

You want to fill a drawn shape with a color.

Solution

Use the beginFill() and endFill() methods of the Graphics class.

Discussion

In the section called “12.3 Defining a Line Style”, you learned how to define a line style, but even a closed line has no fill without specifying a fill style. The beginFill() method specifies a color and opacity to fill any shapes drawn until you call the endFill() method.

When you use a fill, if drawn shapes aren’t closed (meaning the line does not end at its starting point), the shapes are closed for you. To demonstrate this effect, the following code draws a complete triangle even though the instruction that draws the last side of the triangle is commented out (to prevent it from drawing). If you remove the beginFill() and endFill() instructions from this code, then you see only the two lines specified, because the fill process is no longer auto-closing the shape.

var sp:Sprite = new Sprite();
addChild(sp);
sp.x = sp.y = 100;
var g:Graphics = sp.graphics;
g.lineStyle(2, 0xFF0000);
g.beginFill(0x0000FF, 1);
g.moveTo(0, -50);
g.lineTo(50, 50);
g.lineTo(-50, 50);
//g.lineTo(0, -50);
g.endFill();

Pages: 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13

Next Pagearrow