How can I fill an actionscript 3 polygon with a solid color?

Charles Shoults picture Charles Shoults · Oct 9, 2009 · Viewed 8.7k times · Source

I'm building a map editor for a project and need to draw a hexagon and fill it with a solid color. I have the shape correct but for the life of me can't figure out how to fill it. I suspect it may be due to whether the thing is a Shape, Sprite or UIComponent. Here is what I have for the polygon itself:

import com.Polygon;
import mx.core.UIComponent;

public class greenFillOne extends UIComponent {
    public var hexWidth:Number = 64;
    public var hexLength:Number = 73;

    public function greenFillOne() {
        var hexPoly:Polygon = new Polygon;
        hexPoly.drawPolygon(40,6,27+(hexWidth*.25),37,0x499b0e,1,30);
        addChild(hexPoly);
    }
}

Answer

Glenn picture Glenn · Oct 9, 2009

The Polygon class isn't a standard Adobe library, so I don't know the specifics. However, assuming that it uses the standard flash API, it should be no problem to add some code to extend the function. You just need to make sure you're doing a graphics.beginFill before the graphics.lineTo / graphics.moveTo functions. And then finish with graphics.endFill.

e.g.,

var g:Graphics = someShape.graphics;
g.beginFill(0xFF0000,.4); // red, .4 opacity 
g.moveTo(x1,y1);
g.lineTo(x2,y2);
g.lineTo(x3,y3);
g.lineTo(x1,y1);
g.endFill();

This will draw a triangle filled with .4 red.