Archive for the ‘Actionscript’ Category

I couldnt believe the Math.round() function in AS3 is so crippled. All it returns is the nearest integer value and ofcourse there is no way you can specify what is the precision of rounding. To make it worse it always rounds 1.5 to 1. Hmmm..

But the solution to this problem is pretty simple.  For example if u want a 2 decimal place rounding all we have to do is multply with 100,then round it and then divide it by 100. Confusing..well here is the code:

public static function roundDecimal(num:Number, precision:int):Number{

var decimal:Number = Math.pow(10, precision);

return Math.round(decimal* num) / decimal;

}

Well that was pretty simple right. Now go coding..all the best.

Unlike C and java where u can declare an m x n array simply by writing arr[m][n], its a bit difficult in actionscrpit3.
It was like u have to make an array of arrays:

new Array(new Array(),new Array(),…)

But a little bit effort can find a way out of this mess. You may make use of the following snippet:

function arrayTest(){

var m:int = 2;
var n:int = 2;
var mainArr:Array = new Array(m);
var i:int;
var j:int;
for (i = 0; i < m; i++) {

mainArr[i] = new Array(n);
for (j = 0; j < n; j++) {

mainArr[i][j] = “[” + i + “][” + j + “]”;

}

}
trace(mainArr);

}

It is a bit of a mess but it will work for sure..

Sometimes you need to pass more than one arguements other than the “event” object. In that case you can use the following code:

//–Listener Function–//

private function listenerFunction(e:MouseEvent,passedVariable:Object): void {
        trace(passedVariable.name);
 }

---------------------------------------------------------------------

//--Caller--//
private function callerFunction(): void {
      var callerObject:Object = {name:"I have a name for sure..."};

            someObject.addEventListener(MouseEvent.CLICK,function (e:MouseEvent) : void {
                  listenerFunction(e,callerObject);
            });
}

Hope that was useful.