PDA

View Full Version : Fractal Tree



UrbaNomad
June 18th, 2003, 02:37 PM
I've been trying to understand the code for ilyas usal's fractal tree for several days now ( http://www.kirupa.com/lab/fractal_tree.htm )
What I dont get is how he makes it grow over time.. I am guessing it has something to do with line 96:
if (this.count++ < maxLength){
But it seems like there should be a for loop there?
If anyone can shine some light I would be very appreciative. :m:

RvGaTe
June 18th, 2003, 03:21 PM
if (this.count++ < maxLength){

is actually a for loop if you know how it works

this.count++

is actually, use the current value, after using, increase it by 1....

++this.count, is the opposite

increase the value, and then use it....
to understand what im trying to say here, try putting this in an empty move

x=1
trace(x++)

and

x=1
trace(++x)

the first will come up with 1 and the second with 2....


so if we take a closer look at that line, it will increase this.count everytime its being used, but it dozn't stay inside the "if" like a for loop....

and becoz its inside this clip event:

mc.onEnterFrame=function(){

it will repeat every frame :)

and there ya have it, a loop :D

pom
June 19th, 2003, 05:52 AM
Basically, it works like that:
this.count refers to the current length of the branch. The maximal length is defined when the branch is created.
Now each branch has it's own onEnterFrame that makes it grow. So we check onEnterFrame if we've reached the total length of the branch, we draw a little piece of the branch, otherwise we kill the onEnterFrame.

pom :)

UrbaNomad
June 19th, 2003, 11:18 AM
Awesome, thanks guys.
Another question...

How come, when I duplicate a movieclip, and I want to move it, I have to do this:

mc.onEnterFrame=function(){
if (this.count++ < maxLength){
this._x = 400;
this._y = 400;
}
}
else delete this.onEnterFrame;


But when I make an empty one and just define the linetype, I can do this:

mc.moveTo(x,y);

It is very strange to me, why cant I juse use moveTo for everything? :x

pom
June 19th, 2003, 12:20 PM
I'm sorry, I don't understand what you're trying to do. Those 2 pieces of code are completely different, aren't they? And in the first one, you're positionning the clip at the same position onEnterFrame :-\

pom :phil:

UrbaNomad
June 19th, 2003, 12:49 PM
Ok I think I get it now. In the fractal tree code, there is a movie clip that is used for DRAWING, and there is a child movie clip of that which is a duplicate of the fruit. The drawing uses moveTo and the child (the duplicate of the fruit) changes the actual _x and _y? I guess that is what it is.
So moveTo is for drawing and _x and _y is for objects.

pom
June 19th, 2003, 03:55 PM
That's right, moveTo is used to move the virtual pen position (you draw from that point), whereas _x and _y are movie clip properties (they're the coordinates of the clip).