Archive for the 'Snippets' Category
[AS3 SNIPPET] If else short form (ternary/tertiary operator)
var visits:Number = 10;
var action:String = (visits>=1) ? "Welcome back" : "Hi first time visitor";
trace(action); //traces ‘Welcome back’
Source: http://www.actionscript4.me/articles/as3-snippet-if-else-conditional-written-short/
[AS3 SNIPPET] Webcam supported modes
function webcamSupportedModes(minW:Number, minH:Number, maxW:Number, maxH:Number, fps:Number, step:uint = 20)
{
var cam:Camera = Camera.getCamera();
for (var w:uint = minW; w < maxW + 1; w = w + step)
{
for (var h:uint = minH; h < maxH + 1; h = h + step)
{
cam.setMode(w, h, fps);
if (w == cam.width && h == cam.height)
{
trace(cam.width + " x " + cam.height + " @ " + cam.fps);
}
}
}
}
webcamSupportedModes(320, 240, 640, 480, 15, 100);
[AS3 SNIPPET] Object to URLVariables
function objectToURLVariables(parameters:Object):URLVariables
{
var paramsToSend:URLVariables = new URLVariables();
for (var i:String in parameters)
{
if (i != null)
{
if (parameters[i] is Array)
{
paramsToSend[i] = parameters[i];
}
else
{
paramsToSend[i] = parameters[i].toString();
}
}
}
return paramsToSend;
}
[AS3 SNIPPET] Quick benchmark a function
function benchmark(func:Function):void
{
var startTime:Date = new Date();
func();
var endTime:Date = new Date();
var benchmark:Number = endTime.time - startTime.time;
var result:String = benchmark + "ms";
trace("result: " + result);
}
function test():void
{
var counter:int = 100000000;
while (counter > 0)
{
counter--;
}
}
benchmark(test);
[AS3 SNIPPET] Who called my actionscript method?
function methodToCall():void
{
calledMethod();
}
function calledMethod():void
{
try
{
throw new Error("my error");
}
catch (e:Error)
{
trace(e.getStackTrace());
}
}
methodToCall();
Source: http://blog.comtaste.com/2008/11/how_to_know_who_called_my_acti.html







