AS3:beginGradientFIll() 不会使我成为渐变!

发布于 2024-11-18 09:05:31 字数 419 浏览 4 评论 0原文

我正在尝试用径向渐变渲染一个圆,但我似乎无法弄清楚。

var bkgdGrad:Shape = new Shape();
bkgdGrad.graphics.beginGradientFill(GradientType.RADIAL, [0x0000FF, 0x00FF00], [1, 1], [0, 255],null,"pad");
bkgdGrad.graphics.drawCircle(0,0,r+200);
bkgdGrad.graphics.endFill();
this.addChild(bkgdGrad);

上面的代码为我呈现了一个实心的绿色圆圈。如果我将颜色后面的数组更改为 [1,0] (alpha 数组),我会得到透明填充。我似乎无法让 flash 绘制径向渐变,上面的方法对于 GradientType.LINEAR 没有问题

I'm trying to render a circle with a radial gradient but I can't seem to figure it out.

var bkgdGrad:Shape = new Shape();
bkgdGrad.graphics.beginGradientFill(GradientType.RADIAL, [0x0000FF, 0x00FF00], [1, 1], [0, 255],null,"pad");
bkgdGrad.graphics.drawCircle(0,0,r+200);
bkgdGrad.graphics.endFill();
this.addChild(bkgdGrad);

The above code renders a solid green circle for me. If I change the array after the colors to [1,0] (the alpha array) I get a transparent fill. I can't seem to get flash to draw a radial gradient, the above works no problem for GradientType.LINEAR

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

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

发布评论

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

评论(3

逆光下的微笑 2024-11-25 09:05:31

试试这个,也许这会对你有帮助:

package  
{  
    import flash.display.Sprite;  
    import flash.display.GradientType;  
    import flash.geom.Matrix;  

    public class RadialGradient extends Sprite  
    {  
        private var gType:String;  
        private var matrix:Matrix;  

        private var bound:Sprite;  

        public function RadialGradient()  
        {  
            var gType:String = GradientType.RADIAL;  

            var matrix:Matrix = new Matrix();  
            matrix.createGradientBox(550,400,0,0,0);  

            var gColors:Array = [0x0000FF, 0x00FF00];  
            var gAlphas:Array = [1,1];  
            var gRatio:Array = [0,255];  

            var bound:Sprite = new Sprite();  
            bound.graphics.beginGradientFill(gType,gColors,gAlphas,gRatio,matrix);  
            bound.graphics.drawCircle(0,0,r+200);
            bound.x = bound.y = 0;  
            addChild(bound);  
        }  
    }  
}  

Try this, it may be this will help you:

package  
{  
    import flash.display.Sprite;  
    import flash.display.GradientType;  
    import flash.geom.Matrix;  

    public class RadialGradient extends Sprite  
    {  
        private var gType:String;  
        private var matrix:Matrix;  

        private var bound:Sprite;  

        public function RadialGradient()  
        {  
            var gType:String = GradientType.RADIAL;  

            var matrix:Matrix = new Matrix();  
            matrix.createGradientBox(550,400,0,0,0);  

            var gColors:Array = [0x0000FF, 0x00FF00];  
            var gAlphas:Array = [1,1];  
            var gRatio:Array = [0,255];  

            var bound:Sprite = new Sprite();  
            bound.graphics.beginGradientFill(gType,gColors,gAlphas,gRatio,matrix);  
            bound.graphics.drawCircle(0,0,r+200);
            bound.x = bound.y = 0;  
            addChild(bound);  
        }  
    }  
}  
才能让你更想念 2024-11-25 09:05:31

您需要一个 Matrix 对象及其 createGradientBox() 方法。

我创建了一个名为 RadialGraident 的类,它创建一个 Shape 对象,其中包含一个具有径向渐变的圆。您需要做的就是在创建对象时解析半径、颜色、alpha 和比率,如下例所示:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var radialGradient:RadialGradient = new RadialGradient(200, [0x0000FF, 0x00FF00], [1, 1], [0, 255]);
            addChild(radialGradient);

        }// end function

    }// end class

}// end package

import flash.display.GradientType;
import flash.display.Shape;
import flash.geom.Matrix;

internal class RadialGradient extends Shape
{   
    public function RadialGradient(radius:Number, colors:Array, alphas:Array, ratios:Array)
    {
        var matrix:Matrix = new Matrix();
        matrix.createGradientBox(radius * 2, radius * 2);
        graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);
        graphics.drawCircle(radius, radius, radius);
        graphics.endFill();

    }// end function

}// end class

You need a Matrix object as well as its createGradientBox() method.

I've made a class called RadialGraident that creates a Shape object with a circle that has a radial gradient. All you need to do is parse the radius, colors, alphas and ratios upon creating the object like in the following example:

package 
{
    import flash.display.Sprite;
    import flash.events.Event;

    public class Main extends Sprite 
    {
        public function Main():void 
        {
            if (stage) init();
            else addEventListener(Event.ADDED_TO_STAGE, init);

        }// end function

        private function init(e:Event = null):void 
        {
            removeEventListener(Event.ADDED_TO_STAGE, init);

            var radialGradient:RadialGradient = new RadialGradient(200, [0x0000FF, 0x00FF00], [1, 1], [0, 255]);
            addChild(radialGradient);

        }// end function

    }// end class

}// end package

import flash.display.GradientType;
import flash.display.Shape;
import flash.geom.Matrix;

internal class RadialGradient extends Shape
{   
    public function RadialGradient(radius:Number, colors:Array, alphas:Array, ratios:Array)
    {
        var matrix:Matrix = new Matrix();
        matrix.createGradientBox(radius * 2, radius * 2);
        graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);
        graphics.drawCircle(radius, radius, radius);
        graphics.endFill();

    }// end function

}// end class
ˉ厌 2024-11-25 09:05:31

使用新的矩阵创建渐变框 对象并将新的矩阵对象分配给您的 beginGradientFill matrix 参数:

flash.geom.Matrix.createGradientBox ()


[编辑]:这是一个简单的在线教程,应该有助于解释更多信息:

在 AS3 中以编程方式绘制渐变

create a gradient box with a new Matrix object and assign the new matrix object to your beginGradientFill matrix parameter:

flash.geom.Matrix.createGradientBox()


[EDIT]: here's a simple online tutorial that should help explain more:

Drawing Gradients Programatically in AS3

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