Newest Articles

8 Ball Pool
Photo Reel
Image Color Tinting using Actionscript
3d Rotating Image Cube
Image Slider with Easing
Catapult Game


Popular Articles

True Fullscreen Flash Mode
8 Ball Pool
FLV Player
Image Slider
Mp3 Player with XML Playlist
Album Slide


Random Articles

onEnterFrame vs setInterval
Moving Clouds and Waving Grass
FLV Player
Album Slide
Space Shooter Game
Focus in on an Image


Links

Foundation-Flash
Tutorial4Me
MickM
TutorialQuest
Tutorialsphere.com - Free Online Tutorials
Newgrounds
TWiT
Link to SwfSpot
Swf Spot



rss feed

Growing Tree using Recursion

Growing Tree using Recursion
AddThis Social Bookmark Button
Description: Tree grows and sprouts new branches with leaves
Author: John Bezanis
Added: October 31st 2007
Version: Flash 8
Total Views: 10574
Views in the Past 7 Days: 155


This applet demonstrates how trees can be created by using recursion. Recusion is a technique where a function calls itself. The tree starts out with one branch with a leaf on it and grows each frame. There's a small chance of the branch sprouting a child branch. If a branch sprouts its first child, the leaf disappears and a leaf grows out of the new branch. The leaf color is chosen randomly from 4 colors and scaled to different sizes. The tree is completely cleared and redrawn with the start of each frame.

All of the information about the branches is stored in the array "tree". The function drawSegment is the recursive function, and it starts with 1 trunk branch. The start point is always 0,0. The function calls itself to process each of its children, and the children call their children, until there are no more and the function returns. The childs' starting positions on each frame are based on their parents' end positions. The rotation is based on the parent's rotation, with a 90 degree maximum variance. The resettree function clears all of the tree segments and starts it all over again. The source code is available below:

Download the Source File
Questions and Suggestions
Dude that is totally awesomeness
October 31st 2007 07:10AM   -   Xyber
Hey! That'sgreat! How can we make it grow faster? Has anyone got an hint? Thank you!
November 13th 2007 09:11AM   -   Geek
Nice =)
November 26th 2007 00:11AM   -   AsianSpark
is this done with the help of script or just the animation????
November 26th 2007 01:11AM   -   pamsi
cooool..................
November 28th 2007 05:11AM   -   xanuz
very nice, i have a doubt whether it is done with animation or script? anyway it is very nice.
December 12th 2007 07:12AM   -   Anand
i'm trying to make christmas tree growin branch by branch but canot get the right order. it grows verticaly to the top and then strats branching from the top to the bottom (and that's not the way trees are growing)
December 23rd 2007 17:12PM   -   igregurec
This Coooooooooool.............. man
December 26th 2007 22:12PM   -   Abdul Desuza
awesome
January 21st 2008 12:01PM   -   tom
very nice
January 28th 2008 06:01AM   -   kae
This is new idea... Good Job..!
April 17th 2008 17:04PM   -   Ranjith
Anyone figure out how to make it grow faster?
May 19th 2008 11:05AM   -   Rob
This is great! But one comment is that, the tree must grow vertically and not diagonally. Well, i think the farmer didn't put some fence on it that's why it grew out sideways. But hey, great job!
June 3rd 2008 20:06PM   -   Shinobi
Hey, very nice ... It's probabily a very wind landscape..
June 12th 2008 13:06PM   -   mauricio
I want to know the code behind this effect
June 24th 2008 03:06AM   -   beeta
Add a Question or Suggestion
name:
website (optional):
captcha type the characters into the box
message (5000 characters or less):