Hi list...

I thought I'd share code that generates a sprite displaying neverending cloud 
movement with bitmap data, using perlin noise.

- Michael M.

package {
        import flash.display.Sprite;
        import flash.display.Bitmap;
        import flash.display.BitmapData;
        import flash.display.GradientType;
        import flash.display.BlendMode;
        import flash.geom.Matrix;
        import flash.geom.Point;
        import flash.events.Event;
        import flash.geom.Matrix;
        public class Sky extends Sprite {
                private var s:Sprite;
                private var b:Sprite;
                private var perlinOffset:Array;
                private var bd:BitmapData;
                function Sky(w:uint, h:uint) {
                        b = new Sprite();
                        var m:Matrix = new Matrix();
                        m.createGradientBox(w,h,(90*(Math.PI/180)), 0, 0);
                        //0x81ACDC
                        b.graphics.beginGradientFill(GradientType.LINEAR, 
[0x5791D0, 0xFFFFFF], [1,1], [0,255], m);
                        b.graphics.drawRect(0,0,w,h);
                        b.graphics.endFill();
                        addChild(b);
                        s = new Sprite();
                        s.alpha = .7;
                        s.blendMode = BlendMode.OVERLAY;
                        // Offset array for perlin function
                        perlinOffset = [new Point(45, 34)];
                        // Create the bitmapdata we are going to change with 
the perinNoise function
                        bd = new BitmapData(w, h, true, 0x000000);              
        
                        var bmp:Bitmap = new Bitmap(bd);
                        s.addChild(bmp);
                        s.addEventListener(Event.ADDED_TO_STAGE, init);
                        addChild(s);
                }
                private function init(e:Event):void{
                        s.removeEventListener(Event.ADDED_TO_STAGE, init);
                        animateSky(true);
                }
                public function animateSky(activate:Boolean):void{
                        if(activate == true){
                                s.addEventListener(Event.ENTER_FRAME, 
EnterFrame);
                        } else {
                                s.removeEventListener(Event.ENTER_FRAME, 
EnterFrame);
                        }
                }
                private function EnterFrame(e:Event):void{
                        //    change the values in the perlinOffset to animate 
each perlin layer
                        perlinOffset[0].x += 3;
                        perlinOffset[0].y+=.3;
                        //    apply perlin noise to our bitmapdata 
                        s.removeChildAt(0);
                        
//bd.perlinNoise(250,40,7,1,false,false,8,false,perlinOffset);          
                        
bd.perlinNoise(220,40,3,1,false,false,8,false,perlinOffset);
                        s.addChild(new Bitmap(bd));
                }
        }
}

_______________________________________________
Flashcoders mailing list
Flashcoders@chattyfig.figleaf.com
http://chattyfig.figleaf.com/mailman/listinfo/flashcoders

Reply via email to