Author Archives: Zevan

Random Choice

CLICK HERE TO COPY
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 [...]

Posted in functions, random | 2 Comments

Rect vs Ellipse

CLICK HERE TO COPY
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:
CLICK HERE TO COPY
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)];

  [...]

Posted in Graphics, associative arrays, functions, random | Leave a comment

Associative Array Function Call

CLICK HERE TO COPY
Actionscript:

for (var i:int = 0; i<4; i++){

    this["phase"+i]();

}

 

function phase0():void{

    trace("phase 0");

}

function phase1():void{

    trace("phase 1");

}

function phase2():void{

    trace("phase 2");

}

function phase3():void{

    trace("phase 3");

}

 

/*

will output:

phase 0

phase 1

phase 2

phase 3

*/

 

/*

WARNING: This code was written for fun. Use at your own risk.

*/

I was pleasantly surprised back in the AS1 days... when I discovered [...]

Posted in arrays, associative arrays, functions | Leave a comment

setPixel() Cubic Bezier

CLICK HERE TO COPY
Actionscript:

var canvas:BitmapData=new BitmapData(280,280,false,0x000000);

addChild(new Bitmap(canvas, PixelSnapping.AUTO, true));

 

var color:uint;

 

// anchor x1, anchor y1,

// control-handle x2, control-handle y2,

// anchor x3, anchor y3, control-handle x4,

// control-handle y4, [resolution incremental value between 0-1]

function cubicBezier(x1:Number, y1:Number, x2:Number, y2:Number,

                          x3:Number, y3:Number, x4:Number, y4:Number, [...]

Posted in BitmapData, bezier, setPixel | Tagged | Leave a comment