By Zevan | November 27, 2008
Actionscript:
-
var branches:int = 0;
-
var maxBranches:int = 400;
-
-
graphics.lineStyle(0,0x000000);
-
-
makeBranch(300,350,100,-45,45);
-
-
function makeBranch(xp:Number, yp:Number, leng:Number, min:Number, max:Number):void {
-
-
var endX:Number, endY:Number;
-
var theta:Number = (min + Math.random()*(max-min) - 90) * Math.PI / 180;
-
-
endX = xp + leng * Math.cos(theta);
-
endY = yp + leng * Math.sin(theta);
-
-
graphics.moveTo(xp, yp);
-
graphics.lineTo(endX, endY);
-
-
if (branches <maxBranches) {
-
var newLength:Number = leng*.7;
-
setTimeout(makeBranch, 0, endX, endY, newLength, -90, 0);
-
setTimeout(makeBranch, 0, endX, endY, newLength, 0, 90);
-
}
-
branches+=2;
-
}
Draws a tree using recursion.
By Zevan | November 23, 2008
Actionscript:
-
var counter:int = 0;
-
-
graphics.lineStyle(0,0x000000);
-
-
recursive();
-
-
function recursive():void{
-
-
if (counter <4000){
-
graphics.moveTo(10,10);
-
graphics.lineTo(counter, 200);
-
counter+=4;
-
// don't call recursive(), use setTimeout instead
-
setTimeout(recursive,0);
-
}else{
-
trace(counter / 4 + " lines were drawn");
-
}
Be careful with this one, if you don't know what your doing you may be able to crash flash.
Anyway... if you use a recursive function in flash you may find yourself getting a stack overflow error. You can avoid this by using setTimeout():
Actionscript:
-
// don't call recursive(), use setTimeout() instead
-
setTimeout(recursive,0);
Of course... the stack overflow error is there for a reason, if used incorrectly you could bring the flash player to a standstill.
You can also achieve animated recursion by actually adding a delay to your setTimout() call:
Actionscript:
-
// don't call recursive(), use setTimeout() instead
-
setTimeout(recursive,10);
By Zevan | November 2, 2008
Actionscript:
-
function randomChoice(...args:Array):*{
-
return args[int(Math.random()*args.length)];
-
}
-
-
// choose randomly from a number of different values
-
var val:* = randomChoice(10, "Hello", "0xFF0000", 1000);
-
-
// choose a random color (in this case red, green or blue)
-
var randColor:uint = randomChoice(0xFF0000, 0x00FF00, 0x0000FF);
-
-
trace(val);
-
trace(randColor);
The function randomChoice() takes any number of arguments and spits one of them back randomly. Just a handy function to have around... I especially like to use this function to choose from a handful of colors.
By Zevan | November 1, 2008
Actionscript:
-
for (var i:int = 0; i<100; i++){
-
graphics.lineStyle(0,0);
-
graphics[["drawEllipse", "drawRect"][int(Math.random()*2)]](Math.random()*400, Math.random()*300, Math.random()*100,Math.random()*100)
-
}
-
-
/*
-
WARNING: This code was written for fun. Use at your own risk.
-
*/
And a more readable version:
Actionscript:
-
var methodChoices:Array = ["drawEllipse", "drawRect"];
-
var method:String;
-
var xp:Number, yp:Number, w:Number, h:Number;
-
for (var i:int = 0; i<100; i++){
-
method = methodChoices[int(Math.random()*methodChoices.length)];
-
graphics.lineStyle(0,0);
-
xp = Math.random()*400;
-
yp = Math.random()*300;
-
w = Math.random()*100;
-
h = Math.random()*100;
-
graphics[method](xp, yp, w, h)
-
}
-
-
/*
-
WARNING: This code was written for fun. Use at your own risk.
-
*/
Here I use yesterdays associative array function call technique to do something different. Not really all that useful, but interesting....