views:

635

answers:

3

Hey everyone, my first time trying to draw a multi-color gradient in actionscript 3.

So I got this code from the help docs, but I can't seem to get a vertical gradient, whatever formula or number I use for rotate, it stays stuck on the default horizontal gradient :(

You can view my gradient here

My Code:

You can see in the traces the rotation code I tried...

package
{
import flash.display.DisplayObject;
import flash.display.GradientType;
import flash.display.SpreadMethod;
import flash.display.MovieClip;
import flash.display.Graphics;
import flash.display.Sprite;
import flash.display.Shape;
import flash.geom.Matrix;
import flash.events.*;

public class MyGradient extends MovieClip
{
    private var colorHolder:MovieClip = new MovieClip();
    private var colorGrad:MovieClip = new MovieClip();
    private var fillType:String     = GradientType.LINEAR;
    private var colors:Array        = [0xFF0000, 0x4F8EEC];
    private var alphas:Array        = [1, 1];
    private var ratios:Array        = [0x00, 0xFF];
    private var matr:Matrix         = new Matrix();
    private var spreadMethod:String = SpreadMethod.PAD;


    public function MyGradient():void
    {
        if (stage) init();
        else addEventListener(Event.ADDED_TO_STAGE, init);
    }

    private function init():void
    {
        colorHolder = new MovieClip();
        colorGrad   = new MovieClip();

        //matr.rotate(30*Math.PI/180);
        //matr.rotate(45);
        //matr.rotate(90);
        //matr.rotate(Math.PI/90);
        matr.rotate(Math.PI/9);
        matr.createGradientBox(200, 200, 0, 0, 1);
        colorGrad.graphics.beginGradientFill(fillType, colors, alphas, ratios, matr, spreadMethod);        
        colorGrad.graphics.drawRect(0,0,200,200);

        colorHolder.addChild(colorGrad);
        addChild(colorHolder);
        }

    }

}

Updated Fix by Heavily involved:

matr.createGradientBox(200, 200, Math.PI/2, 0, 0);

alt text

+1  A: 

This line of code sets the rotation value back to zero:

matr.createGradientBox(200, 200, 0, 0, 1);

If you look at the parameters accepted by the function you'll see that the third parameter is rotation. So, try the following:

matr.createGradientBox(200, 200, Math.PI/9, 0, 1);

Also, is there any reason you are translating the y value by 1 pixel?

Hope this helps. Good luck!

heavilyinvolved
Ah sweet! :D How come /9 makes it a 45 degree gradient? I tried other numbers, but still can't get it to be a straight up and down vertical gradient :( Oh and the y=1 I just stuck in there for no reason
Leon
If you want a top to bottom gradient use Math.PI/2. It's b/c the Matrix class expects that value in radians. This is a great bit to have next to your computer: http://upload.wikimedia.org/wikipedia/commons/thumb/9/9a/Degree-Radian_Conversion.svg/600px-Degree-Radian_Conversion.svg.png
heavilyinvolved
ah thanks yeah /2 works and thanks for the infographic :D
Leon
+1  A: 

try to change the order of this 2 lines, so it would be:

matr.createGradientBox(200, 200, 0, 0, 1);
matr.rotate((Math.PI/180)*90);
dome
yup that works too :)
Leon
+1  A: 

If you're doing a lot of drawing, you should also check out degrafa.

http://www.degrafa.org/

ablerman
I still haven't made the switch to Flex yet, I guess just waiting on the next version of 'Flash Builder' but thanks I booked marked it :)
Leon