Archive for the 'Snippets' Category

[AS3 SNIPPET] Align DisplayObject to a grid with padding, left-to-right and right-to-left

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
public static function grid(columns : int, rows : int, xSpacing : int, ySpacing : int, xPadding : int, yPadding : int, leftToRight : Boolean = true) : Vector.<Point>
{
    var points : Vector.<Point> = new Vector.<Point>();
    var pt : Point;
    var row : Number;
    var col : Number;
    var num : int = (columns * rows);

    for (var i : int = 0;i < num;i++)
    {
        pt = new Point();

        if (leftToRight)
        {
            row = (i % columns);
            col = Math.floor(i / columns);

            pt.x = (row * (xSpacing + xPadding));
            pt.y = (col * (ySpacing + yPadding));
        }
        else
        {
            row = (i % rows);
            col = Math.floor(i / rows);

            pt.x = (col * (xSpacing + xPadding));
            pt.y = (row * (ySpacing + yPadding));
        }

        points.push(pt);
    }

    return points;
}

[AS3 SNIPPET] Add CDATA to XML

1
2
3
4
public static function addCdata(data : String):XML
{
    return new XML("<![CDATA[" + data + "]]\>");
}

[AS3 SNIPPET] Resize movieclip/image (any DisplayObject) to fill specified area

1
2
3
4
5
6
7
8
9
10
11
public static function resizeToFill(mc : *, maxW : Number, maxH : Number = 0, constrainProportions : Boolean = true) : void
{
    maxH = maxH == 0 ? maxW : maxH;
    mc.width = maxW;
    mc.height = maxH;

    if (constrainProportions)
    {
        mc.scaleX > mc.scaleY ? mc.scaleY = mc.scaleX : mc.scaleX = mc.scaleY;
    }
}

« Previous PageNext Page »