2009-10-08 21 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);