Search code examples
flashactionscript-3graphicsgradientfill

AS3: beginGradientFIll() doesn't make me a gradient!


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


Solution

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