2009-10-08 64 views
1

我正在为一个项目构建地图编辑器,需要绘制六边形并用纯色填充。我的形状是正确的,但对于我的生活无法弄清楚如何填充它。我怀疑这可能是由于这个东西是一个Shape,Sprite或者UIComponent。以下是我对多边形本身的看法:如何使用纯色填充动作3多边形?

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); 
    } 
} 
+0

如果您有权访问com.Polygon类的代码,请在绘图开始之前添加graphics.beginFill(color,alpha)。 – Amarghosh 2009-10-09 04:47:32

+0

我试过这样做,使用com.polygon类。虽然它仍然呈现十六进制的轮廓,但它不会填充十六进制。我尝试了几个地方,包括将填充信息添加到polygon.as,但它不起作用。 – 2009-10-09 16:03:40

回答

6

Polygon类不是标准的Adobe库,所以我不知道具体细节。但是,假设它使用标准的Flash API,添加一些代码来扩展函数应该没有问题。您只需确保在graphics.lineTo/graphics.moveTo函数之前执行graphics.beginFill即可。然后用graphics.endFill完成。

例如,

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(); 

这将绘制填充有0.4红色三角形。

1

我会把它放在这里,因为回答它作为Glenn的评论超过了字符数限制。我的动作文件扩展了UIComponent。当我创建了一个变量hexPoly:Polygon = new Polygon;它会渲染十六进制的轮廓,但无论我做了什么,都不会填充它。我检查了polygon.as并重复了这些方法,但是作为一个精灵并且它工作正常。所以,我需要弄清楚如何将多边形作为精灵包装,或者保持原样。

var hexPoly:Sprite = new Sprite; 
hexPoly.graphics.beginFill(0x4ea50f,1); 
hexPoly.graphics.moveTo(xCenter+(hexWidth*.25)+Math.sin(radians(330))*radius,offset+(radius-Math.cos(radians(330))*radius)); 
hexPoly.graphics.lineTo(xCenter+(hexWidth*.25)+Math.sin(radians(30))*radius,offset+(radius-Math.cos(radians(30))*radius)); 
hexPoly.graphics.lineTo(xCenter+(hexWidth*.25)+Math.sin(radians(90))*radius,offset+(radius-Math.cos(radians(90))*radius)); 
hexPoly.graphics.lineTo(xCenter+(hexWidth*.25)+Math.sin(radians(150))*radius,offset+(radius-Math.cos(radians(150))*radius)); 
hexPoly.graphics.lineTo(xCenter+(hexWidth*.25)+Math.sin(radians(210))*radius,offset+(radius-Math.cos(radians(210))*radius)); 
hexPoly.graphics.lineTo(xCenter+(hexWidth*.25)+Math.sin(radians(270))*radius,offset+(radius-Math.cos(radians(270))*radius)); 
hexPoly.graphics.endFill(); 
addChild(hexPoly);