AS3 Tree
AS3 Tree
by Duarte Peixinho
duarte.peixinho@gmail.com
page: http://duartepeixinho.com
package
{
import flash.display.Sprite;
import flash.events.Event;
import flash.filters.BlurFilter;
import flash.geom.Point;
import flash.display.Shape;
import flash.events.MouseEvent;
[SWF(width = "600", height = "400", backgroundColor = "#ffffff", frameRate = "60")]
/*
*
* AS3 Tree
* by Duarte Peixinho
* duarte.peixinho@gmail.com
* page: http://duartepeixinho.com
*
*/
public class FlashTest extends Sprite
{
private var branche1:Branches;
private var branche2:Branches;
private var branche3:Branches;
public function FlashTest():void {
branche1 = new Branches(Math.random() * 100 + 600, 10, 5, 10, -95, 20);
branche2 = new Branches(Math.random() * 100 + 600, 10, 5, 10, -90, 20);
branche3 = new Branches(Math.random() * 100 + 600, 10, 5, 10, -85, 20);
branche1.x = (stage.stageWidth / 2);
branche1.y = stage.stageHeight;
branche1.filters = [new BlurFilter(2,2)];
addChild(branche1);
branche2.x = (stage.stageWidth / 2);
branche2.y = stage.stageHeight;
branche2.filters = [new BlurFilter(2,2)];
addChild(branche2);
branche3.x = (stage.stageWidth / 2);
branche3.y = stage.stageHeight;
branche3.filters = [new BlurFilter(2,2)];
addChild(branche3);
addEventListener(Event.ENTER_FRAME, processFrame);
stage.addEventListener(MouseEvent.CLICK, clicked);
}
private function clicked(e:MouseEvent):void {
removeChild(branche1);
removeChild(branche2);
removeChild(branche3);
branche1 = new Branches(Math.random() * 100 + 600, 10, 5, 20, -95, 20);
branche2 = new Branches(Math.random() * 100 + 600, 10, 5, 20, -90, 20);
branche3 = new Branches(Math.random() * 100 + 600, 10, 5, 20, -85, 20);
branche1.x = (stage.stageWidth / 2);
branche1.y = stage.stageHeight;
branche1.filters = [new BlurFilter(2,2)];
addChild(branche1);
branche2.x = (stage.stageWidth / 2);
branche2.y = stage.stageHeight;
branche2.filters = [new BlurFilter(2,2)];
addChild(branche2);
branche3.x = (stage.stageWidth / 2);
branche3.y = stage.stageHeight;
branche3.filters = [new BlurFilter(2,2)];
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;
public function Branches(extension:Number, thickness:int, branchesNumber:Number = 5, lineSize:Number = 5, angle:Number = 90, angleInterval:Number = 30 )
{
this.angle = angle;
this.extension = extension;
this.thickness = thickness;
this.thicknessInit = thickness;
this.angleInterval = angleInterval;
this.lineSize = lineSize;
this.branchesNumber = branchesNumber;
}
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);
branches[i - 1].x = posX;
branches[i - 1].y = posY;
addChild(branches[i - 1]);
actualBranches++;
i++;
}
}
for (var k:int = 0; k < branches.length; k++) {
branches[k].processFrame();
}
}
}
}