Using, Flash MX, creating a randomly generated tree. Seems like my math is just funky. it's creating most of the tree fine, but on some degrees, the branches don't get created right. You can view the fla file here: .
You'll see that some of the branches get created by the default branch. Any help here? I'm terrible @ math.
here's the code (all in first frame):
stop();
this.onEnterFrame = null;
Math.toRadians = function (theta) {
return theta * Math.PI / 180;
}
function createBranch(x, y, offsetrot, level) {
defBranch.duplicateMovieClip('branch' + numBranches, numBranches)
obj = this['branch' + numBranches]
obj._x = x;
obj._y = y;
obj._xscale = 5;
obj._yscale = 5;
obj.steps = 3 + random(5);
obj._rotation = offsetrot + (random(50) - 25);
this['branch' + numBranches].onEnterFrame = function() {
if (this.steps > 0) {
var grownum = random(4) + 1;
this._xscale += grownum;
this._yscale += grownum;
this.steps--;
} else if (this.steps == 0 && level < 12) {
var height = Math.acos(Math.toRadians(this._rotation)) * this._yscale;
var width = Math.asin(Math.toRadians(this._rotation)) * this._xscale;
this.steps--;
if (random(4) > 2)
createBranch(this._x+width,this._y - this._height, this._rotation, level+1);
createBranch(this._x+width,this._y - this._height, this._rotation, level+1);
}
}
numBranches++;
}
var numBranches = 0;
for (var count=0; count<5; count++) {
createBranch(200 + (count * 50),250,0,0);
}
You'll see that some of the branches get created by the default branch. Any help here? I'm terrible @ math.
here's the code (all in first frame):
stop();
this.onEnterFrame = null;
Math.toRadians = function (theta) {
return theta * Math.PI / 180;
}
function createBranch(x, y, offsetrot, level) {
defBranch.duplicateMovieClip('branch' + numBranches, numBranches)
obj = this['branch' + numBranches]
obj._x = x;
obj._y = y;
obj._xscale = 5;
obj._yscale = 5;
obj.steps = 3 + random(5);
obj._rotation = offsetrot + (random(50) - 25);
this['branch' + numBranches].onEnterFrame = function() {
if (this.steps > 0) {
var grownum = random(4) + 1;
this._xscale += grownum;
this._yscale += grownum;
this.steps--;
} else if (this.steps == 0 && level < 12) {
var height = Math.acos(Math.toRadians(this._rotation)) * this._yscale;
var width = Math.asin(Math.toRadians(this._rotation)) * this._xscale;
this.steps--;
if (random(4) > 2)
createBranch(this._x+width,this._y - this._height, this._rotation, level+1);
createBranch(this._x+width,this._y - this._height, this._rotation, level+1);
}
}
numBranches++;
}
var numBranches = 0;
for (var count=0; count<5; count++) {
createBranch(200 + (count * 50),250,0,0);
}