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

后端 未结 3 381
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-14 21:44

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.beginGradientFi         


        
3条回答
  •  不思量自难忘°
    2021-01-14 22:23

    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
    

提交回复
热议问题