Posts tagged ‘snippet’

AS3: Random Colour between 2 colour values

Leave a commentJuly 22 | 2009

Useful little snippet to generate a colour between 2 colour values in ActionScript 3

1
2
3
4
5
6
7
8
9
10
11
12
13
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;
}

Snippet: Easy globalToLocal in AS3

2 CommentsApril 7 | 2009

Short little snippet I found the other day to convert coordinates from one movieclip to another in Actionscript 3

1
2
3
4
5
6
function localToLocal(fr:MovieClip, to:MovieClip):Point {
    return to.globalToLocal(fr.localToGlobal(new Point()));
};

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

AS3 Currency Formatter

16 CommentsMarch 26 | 2009

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

Download the Currency Formatter AS3 class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
/* 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;
        }
    }
}