3 Replies Latest reply on Nov 17, 2007 12:43 PM by kglad

    Writing a simple class

    brian914 Level 1
      I would like to write a class that will draw a gradient for the whole background of my file. I had written this in my file and it was working, but I would like to put that code into a class now, as I am trying to learn how to write classes. I am totally new to classes, so bare with me, I don't know what I am doing... more guessing at this point really. Here is what I have so far.

      My class is called BgGradient.as and holds the following AS so far.

      class BgGradient
      var gradient01:String;
      var gradient02:String;

      var fillType:String = "linear";
      var colors:Array = [gradient01, gradient02];
      var alphas:Array = [100, 100];
      var ratios:Array = [0, 255];
      var matrix:Object = {matrixType:"box", x:0, y:0, w:Stage.width, h:Stage.height, r:(90/180)*Math.PI};
      var spreadMethod:String = "pad";
      var interpolationMethod:String = "linearRGB";
      var focalPointRatio:Number = 1;

      function BgGradient() {
      beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio);
      moveTo(0, 0);
      lineTo(Stage.width, 0);
      lineTo(Stage.width, Stage.height);
      lineTo(0, Stage.height);
      lineTo(0, 0);


      I would like gradient01 and gradient02 to be hex values that I can specify in my main file. What am I missing here? Do I need to import anything? How about the drawing API? How would I call this from my main file?

      Thanks a lot for any help with this. I really appreciate any help!
        • 1. Re: Writing a simple class
          kglad Adobe Community Professional & MVP
          pass gradient01 and gradient02, as well as a movieclip (like the main timeline), as parameters in new BgGradient constructor statement. and define color in your constructor.
          • 2. Re: Writing a simple class
            brian914 Level 1
            Would that mean, I have to pass everything single one of the variables into the BgGradient constructor function? Like I said, I a new to this, so pretty confused as of yet.

            Thanks a lot for the help!
            • 3. Re: Writing a simple class
              kglad Adobe Community Professional & MVP
              you don't have to pass anything, except you must pass something that has can reference a graphics class member to which you can apply your begingradientfill method.

              but if you want to specify gradient01 and gradient02 from outside your class, that would be one way to do it. you could also use a setter to define those variables outside your constructor.

              which is best depends upon what you want to do. if you always know those variable values when your create a class member, pass them in the contructor. otherwise, a setter is a good way to go.