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: AS3 Tree

AS3 Tree
by Duarte Peixinho
duarte.peixinho@gmail.com
page: http://duartepeixinho.com
Get Adobe Flash player
by TheCoderMonkey 19 Oct 2011
// forked from peixinho's AS3 Tree
package
{
    import flash.display.Shape;
    import flash.display.Sprite;
    import flash.events.Event;
    import flash.events.MouseEvent;
    import flash.filters.BlurFilter;
    import flash.geom.Point;
    
    [SWF(width = "600", height = "400", backgroundColor = "#ffffff", frameRate = "60")]
    /*
    *
    * AS3 Tree
    * by Duarte Peixinho
    * duarte.peixinho@gmail.com
    * page: http://duartepeixinho.com
    *
    */
    public class trees extends Sprite
    {
        private var branche1:Branches;
        private var branche2:Branches;
        private var branche3:Branches;
        private var season:String;
        
        public function trees():void {
            clicked();
            
            addEventListener(Event.ENTER_FRAME, processFrame);
            stage.addEventListener(MouseEvent.CLICK, clicked);
        }
        
        private function clicked(e:MouseEvent = null):void {                
            if(branche1)removeChild(branche1);
            if(branche2)removeChild(branche2);
            if(branche3)removeChild(branche3);
            branche1 = null;
            branche2 = null;
            branche3 = null;
            
            var rand:int = 4*Math.random()+1;
            
            if(rand == 1) season = Seasons.SPRING;
            if(rand == 2) season = Seasons.SUMMER;
            if(rand == 3) season = Seasons.AUTUMN;
            if(rand == 4) season = Seasons.WINTER;
            
            branche1 = new Branches(Math.random()*(1+600-400)+400, 10, 10*Math.random(), 20, -95, 20, season);
            branche2 = new Branches(Math.random()*(1+600-400)+400, 10, 10*Math.random(), 20, -90, 20, season);
            branche3 = new Branches(Math.random()*(1+600-400)+400, 10, 10*Math.random(), 20, -85, 20, season);
            
            branche1.x = (stage.stageWidth / 2);
            branche1.y = stage.stageHeight;
            addChild(branche1);
            
            branche2.x = (stage.stageWidth / 2);
            branche2.y = stage.stageHeight;
            addChild(branche2);
            
            branche3.x = (stage.stageWidth / 2);
            branche3.y = stage.stageHeight;
            addChild(branche3);
        }
        
        private function processFrame(e:Event):void {
            
            branche1.processFrame();
            branche2.processFrame();
            branche3.processFrame();
            
        }
    }
}

import flash.display.Sprite;

class Branches extends Sprite
{
    
    
    private var angle:Number;
    private var extension:Number;
    private var thickness:int;
    private var thicknessInit:int;
    
    private var angleInterval:Number;
    private var lineSize:Number;
    private var posX:Number = 0;
    private var posY:Number = 0;
    private var extensionComplete:Number = 0;
    
    //branches
    private var branches:Array = new Array();
    private var branchesNumber:Number = 0;
    private var actualBranches:Number = 0;
    private var i:int = 1;
    
    //lineSize calculation
    private var lineDone:Boolean = true;
    private var angleCalc:Number;
    private var posXinit:Number = 0;
    private var posYinit:Number = 0;
    
    private var leafColour:uint = 0x000000;
    private var leaves:Boolean = false;
    private var season:String;
    
    public function Branches(extension:Number, thickness:int, branchesNumber:Number = 5, lineSize:Number = 5, angle:Number = 90, angleInterval:Number = 30, seasons:String = "Spring") 
    {
        
        this.angle = angle;
        this.extension = extension;
        this.thickness = thickness;
        this.thicknessInit = thickness;
        this.angleInterval = angleInterval;
        this.lineSize = lineSize;
        this.branchesNumber = branchesNumber;
        season = seasons;
        
        switch (seasons)
        {
            case Seasons.SPRING:
                
                leafColour = 0xff99ff;
                leaves = true;
                break;
            case Seasons.SUMMER:
                leafColour = 0x00ff00;
                leaves = true;
                break;
            case Seasons.AUTUMN:
                leafColour = 0x00ff00;
                leaves = true;
                break;
            case Seasons.WINTER:
                
                leaves = false;
                break;
        }
    }
    
    public function processFrame():void {
        //draw here
        if (extension>extensionComplete) {
            
            if (lineDone) {
                posXinit = posX;
                posYinit = posY;
                angleCalc = Math.random() * ((angle + angleInterval) - (angle-angleInterval)) + (angle-angleInterval);
                lineDone = false;
                
            }   
            
            graphics.lineStyle(thickness);
            posX += Math.cos(angleCalc * Math.PI / 180) * lineSize;            
            posY += Math.sin(angleCalc * Math.PI / 180) * lineSize;    
            extensionComplete += Math.round(Math.sqrt(Math.pow(posX - posXinit, 2) + Math.pow(posY - posYinit, 2)));
            thickness = thicknessInit - (thicknessInit * extensionComplete / extension);
            graphics.lineTo(posX, posY);
            
            
            
            if (Math.sqrt(Math.pow(lineSize, 2)) < Math.sqrt(Math.pow(posX - posXinit, 2) + Math.pow(posY - posYinit, 2))) {
                lineDone = true;
            }
            
            
            //branches
            if (actualBranches < branchesNumber) {
                //tweak numbers here to create the branches
                if (extensionComplete >= extension / branchesNumber * i) {
                    branches[i - 1] = new Branches(extension/2, thickness, Math.random()*10+5, lineSize/2, Math.random()*(angle-90), 30, season);
                    branches[i - 1].x = posX;
                    branches[i - 1].y = posY;
                    addChild(branches[i - 1]);
                    actualBranches++;
                    i++;
                
                //draws leaves.
                    if(leaves)
                    {
                    graphics.lineStyle(0);
                    graphics.beginFill(leafColour, 1*Math.random());
                    graphics.drawCircle(posX, posY, 3);
                    graphics.endFill();
                    }
                }
            
            }
            
            
            for (var k:int = 0; k < branches.length; k++) {
                branches[k].processFrame();
                
            }            
        }
        
        
    }
}

class Seasons
{
    public static const SPRING:String = "Spring";
    public static const SUMMER:String = "Summer";
    public static const AUTUMN:String = "Autumn";
    public static const WINTER:String = "Winter";
}