Archive for the 'Snippets' Category

[AS3 SNIPPET] Customize SWF properties using Actionscript


[SWF(width="1024", height="768", backgroundColor="#000000", frameRate="60")]

Note: place this code after any import statement

Post to Twitter

[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/

Post to Twitter

[AS3 SNIPPET] Associate identifier to loop block


var i:Number;
var j:Number;
mainLoop: for (i = 0; i < 10; i++)
{
for (j = 0; j < 10; j++)
{
if (i > 3 && j > 3)
{
break mainLoop;
}
}
}

Post to Twitter

[AS3 SNIPPET] Copy text to clipboard


System.setClipboard("Lorem ipsum");

Post to Twitter

[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);

Post to Twitter

[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;
}

Post to Twitter

[AS3 SNIPPET] Detect if the swf is local or online (using regular expression)


var localMode:Boolean = new RegExp("file://").test(loaderInfo.url);

Post to Twitter

[AS3 SNIPPET] Rest parameters


function restParams(value:int, ...items):void
{
for (var i:uint = 0; i < items.length; i++)
{
trace(items[i] + " (" + typeof(items[i]) + ")");
}
}

restParams(11, "Lorem", 123, new Date(), {});

Post to Twitter

[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);

Post to Twitter

[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

Post to Twitter

« Previous PageNext Page »