Newest Articles

MegaCombs
Flash Media Player
XML Driven Pie Chart
Base Defender
Hangman Game
8 Ball Pool


Popular Articles

True Fullscreen Flash Mode
Hangman Game
Catapult Game
8 Ball Pool
3d Rotating Image Cube
Mp3 Player with XML Playlist


Random Articles

3d Rotating Image Cube
8 Ball Pool
Catapult Game
Motion along a Path
Image Color Tinting using Actionscript
Glowing Orb


Links

Shapes the Game
Reddit
Newgrounds
TWiT
Link to SwfSpot
Swf Spot

Contact me on Google+



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


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
Comments Currently Disabled