August 21, 2010
AS3: Easy globalToLocal in AS3

Short little snippet I found the other day to convert coordinates from one movieclip to another in Actionscript 3
function localToLocal(fr:MovieClip, to:MovieClip):Point {
    return to.globalToLocal(fr.localToGlobal(new Point()));
};

//Useage
var localPoint:Point = localToLocal(fromMc,toMc);

August 21, 2010
AS3: Randomise Array method

Found this over at daveoncode and thought i would like to share it:

private function _getRandom(arr:Array):void

    var arr2:Array = new Array();

    while (arr.length > 0) {
        arr2.push(arr.splice(Math.round(Math.random() * (arr.length - 1)), 1)[0]);
    }

    return arr2;
}

August 21, 2010
AS3 dynamic fonts class

Simple class to include dynamic fonts in your flash movie. You can either compile this from your fav AS editor or apply it to an FLA as the document class.

To access the fonts, all you will need to is load in the generated swf into your project and use the fontName value you specify in the class to apply the appropriate font.

It’s also good practice to limit the unicode range to characters you need. Adobe provides a good reference, flash-unicode-table.xml, which you can find inside the Flex SDK 2/frameworks folder.

package {
   
    import flash.display.MovieClip;
    import flash.text.Font;
   
    public class Fonts extends MovieClip {
        [Embed(source=‘C:/WINNT/fonts/DiCnBd.ttf’, fontName=’_dinBold’, unicodeRange=‘U+0021-U+007A,U+00BB,U+00A9,U+2122,U+00AE’)]  
        public static var _dinBold:Class;
        Font.registerFont(_dinBold);
        trace(”_dinBold LOADED”);
       
        [Embed(source=‘C:/WINNT/fonts/arial.ttf’,fontName=’_Arial’, unicodeRange=‘U+0021-U+007A,U+00BB,U+00A9,U+2122,U+00AE’)]  
        public static var _Arial:Class;
        Font.registerFont(_Arial);
        trace(”_Arial LOADED”);
       
        [Embed(source=‘C:/WINNT/fonts/arialbd.ttf’, fontWeight= “bold”,fontName=’_ArialBold’, unicodeRange=‘U+0021-U+007A,U+00BB,U+00A9,U+2122,U+00AE’)]  
        public static var _ArialBold:Class;
        Font.registerFont(_ArialBold);
        trace(”_ArialBold LOADED”);
       
    }
}

August 21, 2010
AS3: Random Colour between 2 colour values

Importing some older content from my wordpress blog:
——————————————————————————————————

import fl.motion.Color;

var color1:uint = 0x03e338;
var color2:uint = 0xbbe303;
var newColor:uint = Color.interpolateColor(color1, color2, randomHue);

private function _randomNumber(low:Number=NaN, high:Number=NaN):Number
{
    var low:Number = low;
    var high:Number = high;

    return Math.random() * (high - low) + low;
}
———————————————————————————————————

August 20, 2010
AS3: Currency Formatter

Here’s a little class I created to convert any number into currency format, which allows specification of decimal places and currency symbol.
————————————————————————————————————-

/* Use:
 *
 * import agi.utils.Format
 *
 * var _format:Format =  new Format();
 *
 * trace out:
 * trace(_format.currency(32783.2397,2,$)); //$32,783.24
 * trace(_format.currency(32783.2397,3,$)); //$32,783.240
 * trace(_format.currency(32783.2397,2,”“)); //32,783.24
 *
 */

package agi.utils
{
    public class Format
    {
        //—————-CONSTRUCTOR—————-//
        public function Format() { }
       
        //—————-PRIVATE METHODS—————-//
        public function currency(num:Number,decimalPlace:Number=2,currency:String=”$”):String
        {
            //assigns true boolean value to neg in number less than 0
            var neg:Boolean = (num < 0);
           
            //make the number positive for easy conversion
            num = Math.abs(num)

            var roundedAmount:String = String(num.toFixed(decimalPlace));
           
            //split string into array for dollars and cents
            var amountArray:Array = roundedAmount.split(”.”);
            var dollars:String = amountArray[0]
            var cents:String = amountArray[1]
           
            //create dollar amount
            var dollarFinal:String = ””
            var i:int = 0
            for (i; i < dollars.length; i++)
            {
                if (i > 0 && (i % 3 == 0 ))
                {
                    dollarFinal = ”,” + dollarFinal;
                }
               
                dollarFinal = dollars.substr( -i -1, 1) + dollarFinal;
            }  
           
            //create Cents amount and zeros if necessary
            var centsFinal:String = String(cents);
           
            var missingZeros:int = decimalPlace - centsFinal.length;
       
            if (centsFinal.length < decimalPlace)
            {
                for (var j:int = 0; j < missingZeros; j++)
                {
                    centsFinal += “0”;
                }
            }
           
            var finalString:String = ””

            if (neg)
            {
                finalString = ”-“+currency + dollarFinal
            } else
            {
                finalString = currency + dollarFinal
            }

            if(decimalPlace > 0)
            {
                finalString += ”.” + centsFinal;
            }
           
            return finalString;
        }
    }
}

Liked posts on Tumblr: More liked posts »