By Zevan | November 3, 2008
Actionscript:
-
for each(var prop:String in describeType(DisplayObject).factory.accessor.@name){
-
trace(prop);
-
}
-
/* outputs
-
scaleY
-
mouseX
-
mouseY
-
mask
-
rotation
-
alpha
-
transform
-
blendMode
-
x
-
root
-
loaderInfo
-
width
-
z
-
rotationX
-
scale9Grid
-
filters
-
rotationY
-
y
-
stage
-
scaleZ
-
parent
-
accessibilityProperties
-
scrollRect
-
rotationZ
-
height
-
name
-
opaqueBackground
-
blendShader
-
cacheAsBitmap
-
visible
-
scaleX
-
*/
I First saw describeType() at senocular's AS3 Tip of the Day on Kirupa.
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....
By Zevan | October 31, 2008
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 that associative array syntax worked along with function calls (why wouldn't it?). There are some interesting tricks you can do with this technique.... will post some later....