Da Fish in Sea

These are the voyages of Captain Observant

Making a Planet (Sphere)

| Comments

In a few of my examples I am using a Sphere class to create the appearance of a planet. This is the code for it, in case you are curious. It uses a gradient fill, which is kinda tricky, so hopefully this example is helpful.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
package {
/**
 * Sphere
 * draws a circle with a radial gradient giving the appearance of a sphere
 * parameters: radius (Number, default=40), color (uint, default=0xff0000)
 */
    import flash.display.Sprite;
    import flash.display.GradientType;
    import flash.geom.Matrix;

    public class Sphere extends Sprite {

        private var radius:Number;
        private var color:uint;

        public function Sphere(radius:Number=40, color:uint=0xff0000){
            this.radius = radius;
            this.color = color;
            init();
        }

        private function init():void{
            var colors:Array = [0xffffff, color];
            var alphas:Array = [1,1];
            var ratios:Array = [0, 255];
            var matrix:Matrix = new Matrix();
            //this is a normal matrix - gradient is centered & same width as circle
            //matrix.createGradientBox(radius*2,radius*2,0,-radius,-radius);
            matrix.createGradientBox(radius*1.5,radius*1.5,0,-radius,-radius);
            graphics.beginGradientFill(GradientType.RADIAL, colors, alphas, ratios, matrix);
            graphics.drawCircle(0, 0, radius);
            graphics.endFill();
        }
    }
}

// Copyright (c) 2008 David Wilhelm
// MIT license: http://www.opensource.org/licenses/mit-license.php