Posts Tagged ‘as3’

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.

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.