A list of quick and usefull tricks :)

FAST COMPUTING

Fast sign change

i = -i;
i = ~i + 1; // faster
i = (i ^ -1) + 1; // fastest

Fast modulo
If the divisor is a power of 2, the modulo operation can be done with : modulus = numerator & (divisor – 1);

mod = 131 % 4;
mod = 131 & (4 - 1); // faster

Fast modulo lead to Fast parity test

isEven = (i % 2) == 0;
isEven = (i & 1) == 0; // faster

Fast absolute value

absX = Math.abs(x);
absX = x < 0 ? -x : x; // faster
absX = (x ^ (x >> 31)) - (x >> 31); // fastest

Fast distance comparaison
Compare squared distance instead of real distance


ARRAYS
1-line shuffle method

public function shuffle(array:Array):Array { for (var j:int, t:Number, i:int = array.length, a:Array = array.slice(); i; j = Math.random() * i, t = a[--i], a[i] = a[j], a[j] = t){} return a; }

IMAGES

Color channels manipulation using bitshift operators
32 bits

a = color >>> 24;
r = color >>> 16 & 0xFF;
g = color >>>  8 & 0xFF;
b = color & 0xFF;
color = a << 24 | r << 16 | g << 8 | b;

24 bits

r = color >> 16;
g = color >> 8 & 0xFF;
b = color & 0xFF;
color = r << 16 | g << 8 | b;

Grayscale colorMatrixFilter

var rLum:Number = 0.2225; // 0.298912
var gLum:Number = 0.7169; // 0.586611
var bLum:Number = 0.0606; // 0.114477
var matrix:Array = new Array();
matrix = matrix.concat([rLum, gLum, bLum, 0, 0]);
matrix = matrix.concat([rLum, gLum, bLum, 0, 0,]);
matrix = matrix.concat([ rLum, gLum, bLum, 0, 0]);
matrix = matrix.concat([0, 0, 0, 1, 0]);
myDisplayObject.filters = [new ColorMatrixFilter(matrix)];

hsv2rgb

private function hsv2rgb(h:Number,s:Number,v:Number):uint{
    var i:int = int(h/60);
    var f:Number = h/60-i;
    var p:Number = v*(1-s);
    var q:Number = v*(1-s*f);
    var t:Number = v*(1-s*(1-f));
    switch (i) {
        case 0: return v * 0xFF << 16 | t * 0xFF << 8 | p * 0xFF << 0;
        case 1: return q * 0xFF << 16 | v * 0xFF << 8 | p * 0xFF << 0;
        case 2: return p * 0xFF << 16 | v * 0xFF << 8 | t * 0xFF << 0;
        case 3: return p * 0xFF << 16 | q * 0xFF << 8 | v * 0xFF << 0;
        case 4: return t * 0xFF << 16 | p * 0xFF << 8 | v * 0xFF << 0;
        case 5: return v * 0xFF << 16 | p * 0xFF << 8 | q * 0xFF << 0;
    }
    return 0;
}

private function rgb2hsv(rgbColor:uint)
{
    var r:int = rgbColor >> 16;
    var g:int = rgbColor >> 8 & 0xFF;
    var b:int = rgbColor & 0xFF;

    var h:int;
    var s:int;
    var v:int;

    var max = Math.max(r, Math.max(g, b));
    var min = Math.min(r, Math.min(g, b));

    if (r == max)
    {				
        h = (g - b) / (max - min);				
    }
    else if (g == max)
    {
        h = 2 + (b - r) / (max - min);				
    }
    else if (b == max)
    {				
        h = 4 + (r - g) / (max - min);				
    }

    h *= 60;

    if (h < 0) {				
        h += 360;				
    }

    s = Math.max(r, Math.max(g, b));			
    v = (max - min) / max;

    return h << 16 | s << 8 | v;
}

ImageDecodingPolicy.ON_LOAD

var loaderContext:LoaderContext = new LoaderContext();
loaderContext.imageDecodingPolicy = ImageDecodingPolicy.ON_LOAD;
var loader:Loader = new Loader();
loader.load( new URLRequest(IMAGE_URL), loaderContext );

Embed external assets with compression without Flash IDE

[Embed(source="../assets/star.png", mimeType="image/png", compression=true, quality=80)]

Get bounds of an image ignoring transparent pixels

var bounds:Rectangle = myBitmpData.getColorBoundsRect(0xFFFFFFFF, 0x000000, false);

AS3
Namespace

public namespace French;
public namespace Dutch;

English function sayHello():String {
     return "Hello";
}
French function sayHello():String {
     return "Bonjour";
}
French::sayHello();

Labels

Deep copy

public static function clone(source:Object):* {
     var copier:ByteArray = new ByteArray();
     copier.writeObject(source);
     copier.position = 0;
     return copier.readObject();
}

Quick print routine

var pj:PrintJob = new PrintJob();
if (pj.start()) {
     // Resize
     var scale:Number = Math.min(pj.pageWidth / my_sprite.width, pj.pageHeight / my_sprite.height);
     my_sprite.scaleX = my_sprite.scaleY = scale;
     // send result
     pj.addPage(my_sprite);
     pj.send();
}

fl.motion PACKAGE
Code API for color effect panel in Flash IDE
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/motion/AdjustColor.html

Cool collection of method to handle Matrix
http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/fl/motion/MatrixTransformer.html
exemple : Rotation around a point


OTHERS

Constraint value

angle360 = ((angle % 360) + 360) % 360;
angle180 = ((angle % 360) + 540) % 360 - 180;

Quick and simple easing

displayObject.x += (targetX – displayobject.x) * easingFactor;
displayObject.y += (targetY – displayobject.y) * easingFactor;

Approximation of PI

22/7 
355/113
103993/33102

Approximation of the golden number

987/610

Vote in HexoSearch