In case Flash no longer exists; a copy of this site is included in the Flashpoint archive's "ultimate" collection.

Dead Code Preservation :: Archived AS3 works from wonderfl.net

forked from: Random Green Tree

Works slowly 80 trees will not be generate yet all
Get Adobe Flash player
by Sinred 01 Apr 2009
// forked from makc3d's Random Green Tree
//Works slowly 80 trees will not be generate yet all
package{ 
	 import flash.display.Bitmap;
	 import flash.display.BitmapData;
	 import flash.display.BlendMode;
         import flash.display.Sprite;
	 import flash.display.StageAlign;
	 import flash.display.StageScaleMode;
	 import flash.events.Event;
	 import flash.events.MouseEvent;
	 import flash.filters.BlurFilter;
	 import flash.filters.ColorMatrixFilter;
	 import flash.geom.ColorTransform;
	 import flash.geom.Matrix;
	 import flash.geom.Point;
	 import flash.geom.Rectangle;
	 
	 [SWF(backgroundColor = "#000000", frameRate = "12",width="640",height="480")] 
     public class tree extends Sprite {
	    private var level:int = 0;
		private var _sp:Sprite;
	    private var bmp:Array;
		private var maxtrees:int = 80;
		


         private function drawTree (x:Number, y:Number, length:Number, angle:Number, cf:int):void {
             
             
             var destx:Number = x + length * Math.cos(angle * (Math.PI/180));
             var desty:Number = y + length * Math.sin(angle * (Math.PI / 180));
			 
             if (destx > -180 && destx < 180 && destx > -380)
             {
			 level += 1;
			
             if (level ==9)
             {
				if (Math.random () < 0.4) cf = 0x87003F else cf = 0xaafffdfd;
			 }
			 
             with (_sp.graphics){
             lineStyle(1 + 5 / level, cf,level==9?0.7:1);
             moveTo (x, y);
             lineTo (destx, desty);
			 }
			 
             if (level < 9) {
                 drawTree (destx, desty, length * (1 + 3 * Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
                 drawTree (destx, desty, length * (1 + 3 * Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
                 drawTree (destx, desty, length * (1 + 3 * Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
             } else if (level == 9)
             {
				  drawTree (destx, desty, length * (1 + Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
				  drawTree (destx, desty, length * (1 + Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
				  drawTree (destx, desty, length * (1 + Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
				  drawTree (destx, desty, length * (1 + Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
				  drawTree (destx, desty, length * (1 + Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
                  drawTree (destx, desty, length * (1 + Math.random()) * 0.25,angle + 60 * (Math.random() - Math.random()), cf);
			}
			level -= 1;
            }
             
			 
         }
         public function drawTrees()
         {
			 if ((bmp.length) <= maxtrees)
			 {
				 _sp.graphics.clear();
				 drawTree ( 0, 0, 60, -90, 0x251500);
				 bmp.push(new Bitmap(new BitmapData(400, 400, true, 0x00000000)));
				 bmp[bmp.length - 1].bitmapData.draw(_sp,new Matrix(1,0,0,1,200,400))
				 addChildAt(bmp[bmp.length - 1],0);
				 bmp[bmp.length - 1].x = (Math.random() * (stage.stageWidth+1200))-800;
				 bmp[bmp.length - 1].y = stage.stageHeight - 400;
				 bmp[bmp.length - 1].z = 4000;
				 Bitmap(bmp[bmp.length - 1]).bitmapData.fillRect(new Rectangle(197, 398, 6, 2), 0xff000000);
				 for (var i = 1; i < 30; i++) bmp[bmp.length - 1].bitmapData.setPixel(197 + Math.random() * 6, 398 - Math.random() * 8, 0x000000);
				 Bitmap(bmp[bmp.length - 1]).bitmapData.applyFilter(Bitmap(bmp[bmp.length - 1]).bitmapData, new Rectangle(197, 340, 6, 60), new Point(197,340), new BlurFilter(0,6,1));
				 bmp[bmp.length - 1].filters=[new ColorMatrixFilter([0, 0, 0, 0, 0,0, 0, 0, 0, 0,0, 0, 0, 0, 0,0, 0, 0, 1, 0]) ]
			 }
		 }
         public function tree() {
			stage.scaleMode = StageScaleMode.NO_SCALE;
			stage.align = StageAlign.TOP_LEFT;
            
			/*graphics.beginFill(0xffffff);
			graphics.moveTo( -400, stage.stageHeight);
			graphics.lineTo( 230, stage.stageHeight / 2+12);
			graphics.lineTo( 410, stage.stageHeight / 2+12);
			graphics.lineTo( stage.width + 400, stage.stageHeight);
			graphics.lineTo( -400, stage.stageHeight);
			graphics.endFill()*/
			
			 bmp = [];
			 _sp = new Sprite;
			 var back:Sprite = new Sprite();
			 back.graphics
			 
             drawTrees()
			 stage.addEventListener(Event.ENTER_FRAME, function()
			 {
				 drawTrees()
				 var maxz:Number = bmp[bmp.length - 1].z
				 var zmul:Number =bmp.length <= maxtrees?250:20;
				 for (var i = 0; i<bmp.length ; i++)
				 {
					 
					 var b:Number = bmp[i].z - zmul;
					 var d:Number = bmp[i].z / 200
					 var d2:Number = 1-d/20;
					 var matrix:Array = new Array();
                     matrix = matrix.concat([d2, 0, 0, 0, 0]); // red
                     matrix = matrix.concat([0, d2, 0, 0, 0]); // green
                     matrix = matrix.concat([0, 0, d2, 0, 0]); // blue
                     matrix = matrix.concat([0, 0, 0, 1, 0]); // alpha
                     
					 if(b >=0){
					   bmp[i].z = b;
					   //bmp[i].filters = [new BlurFilter(d, d, 1)]
					   bmp[i].filters=[new ColorMatrixFilter(matrix) ]
					 }else
					 {
					   bmp[i].z = 4000;
                                           addChildAt(bmp[i], 0);
					   bmp[i].filters=[new ColorMatrixFilter([0, 0, 0, 0, 0,0, 0, 0, 0, 0,0, 0, 0, 0, 0,0, 0, 0, 1, 0]) ]
					  
					 }
					 
				 }
			});
         }
     }
    }