如何用纯色填充 ActionScript 3 多边形?

发布于 2024-08-07 08:53:35 字数 504 浏览 5 评论 0原文

我正在为一个项目构建一个地图编辑器,需要绘制一个六边形并用纯色填充它。我的形状是正确的,但我一生都不知道如何填充它。我怀疑这可能是由于该东西是 Shape、Sprite 还是 UIComponent。这是我对多边形本身的了解:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

楠木可依 2024-08-14 08:53:35

Polygon 类不是标准的 Adob​​e 库,所以我不知道具体细节。不过,假设它使用的是标准的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 红色填充的三角形。

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.

陌伤浅笑 2024-08-14 08:53:35

我将其放在这里是因为将其作为对格伦的评论来回答超出了字符限制。我的动作脚本文件扩展了 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);

I'll put this here because answering it as a comment to Glenn goes past the character limit. My actionscript file extends UIComponent. When I created a variable hexPoly:Polygon = new Polygon; it would render the outline of the hex, but would not fill it no matter what I did. I examined polygon.as and duplicated the methods, but as a sprite and it worked. So, I need to figure out how to wrap the polygon as a sprite, or just leave it as is.

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文