Util Functions

public static function randomRange(minNum:Number, maxNum:Number):Number{
    return (Math.floor(Math.random() * (maxNum – minNum + 1)) + minNum);
}

Making line breaks work in htmlText in ActionScript 3.0

Common problem had when using HTML text is that the <BR> tags don’t work unless you add “multiline = true”

var someText:TextField = new TextField();
someText.multiline = true;
someText.htmlText = "1st line.<br> 2nd. <br> 3rd.";
addChild(someText);

Change a movieClips framerate.

stage.frameRate = 20;

WARNING: This changes the framerate of EVERY movie clip no the stage.

 

Remove All Children from a MC

while(this.numChildren > 0) this.removeChildAt(0);

Vector (not graphic) Construction

At work I wanted to create a Vector with elements being passed into it (rather than pushing after instantiation) to do this one must:

private var myVector:Vector.<someType> = Vector.<someType>([someType1, someType2]);

To the more adept out there you will notice you are technially casting an array of “someTypes” into the Vector.

 

Stop All Playing Sounds

In AS2 you had StopAllSounds. In AS3 you have this:

import flash.media.SoundMixer;
SoundMixer.stopAll();

Is not the best method but works 100% of the time

Fastest way to get the min or max in an array of ints

var myArray:Array = [2,3,3,4,2,2,5,6,7,2];
var maxValue:Number = Math.max.apply(null, myArray);
var minValue:Number = Math.min.apply(null, myArray);

AS2 vs AS3

I got asked recently the differences between AS2 and AS3. Since it had been years since I had done anything in AS2 I stumbled and only remember a few of the awesome things added. Here is a running list (will update as I think of them):

1. E4X XML parsing – Built in “dot notation” and XML Class means you no longer have to write your own parser and adds a ton of methods for easy extraction of data from XML object.
2. Runtime exceptions (Error messages when errors happen while running the SWF) AS2 would fail silently.
3. Sealed Classes – A sealed class possesses only the fixed set of properties and methods that were defined at compile-time. Stricter compile-time checking means more robust programs. Improves memory usage by not requiring an internal hash table for each object instance.
4. Regular Expressions – String manipulation on steroids.
5. New Primitive Types – int, uint for faster math and color math (manipulating 0xFF0000)
6. ByteArray and Bit Operators – Allows for bit level manipulation of data of JPEGs, Sounds, Files, etc and Bit Operators (for faster math and bit math)
7. Sprites – Light weight display objects (MovieClip extends from these)
8. MovieClips are now dynamically instantiated on the stage by using the linkage and addChild, removeChild in the library rather than duplicateMovieClip, swapDepths, etc.
9. AS3 Loader class with its event model (and addChild) replaces the AS2 loadMovie(“myExternalMovie.swf”, myContainer).
10. unloadAndStop, cacheToBitmap – awesome functions learn and respect their power.
11. Sound Class in AS3 allows for visualizing sound data, reading ID3 Metadata, allows more control over separate sounds and uses the event model rather than .onSoundComplete method of AS2.
12. Method closures – In AS2 an method would not remember what object instance it was fired from and would not keep variables hidden from other instances of itself. The Delegate class was often used to remember the first problem. AS3 does away with this problem by remembering who fired it and which instance fired it. Two examples:

Example 1 – Instanced Variables (newCounter is a function that just incremements a variable)
public function init():void{
var counter1:Function = newCounter();
trace( counter1() ); //1
trace( counter1() ); //2
var counter2:Function = newCounter();
trace( counter2() ); //1
trace( counter1() ); //4 –> scope of i is still with counter1…cool! 🙂
}
(source: http://ted.onflash.org/2010/01/simple-method-closure-in-as3.php)

Example 2 – Remembering what fired it:
AS2 Delegate
myButton.addEventListener(“click”, Delegate.create(this, someMethod));
Delegate.create(this, someMethod)

AS3 Version
myButton.addEventListener(“click”, someMethod);

Bitwise Operators

Bitwise Operators in AS3. In laymans terms they are bit based math operations that are very very fast (up to 600% faster in some cases) but can only be used in few cases. Below “R:” means the regular mathematical equation and “B:” means the bitwise version.

MULTIPLICATION
R: x = x * 2;
B: x = x * 64; //64 = 2^6
R: x = x >> 1;
B: x = x >> 6;

MOD (%) USED TO CHECK DIVIDED BY X
R: isEven = (i % X) == 0;
B: isEven = (i & (X – 1)) == 0;

ABSOLUTE VALUE
R: ii = Math.abs(x);
B: ii = (x ^ (x >> 31)) – (x >> 31);

MISC. TRICKS
//24bit Color extraction
var hexColor:uint = 0x336699;
var red:uint = hexColor >> 16;
var green:uint = hexColor >> 8 & 0xFF;
var blue:uint = hexColor & 0xFF;

//32bit Color extraction (which includes alpha channel)
var hexColor:uint = 0xff336699;
var alpha:uint = hexColor >>> 24;
var red:uint = hexColor >>> 16 & 0xFF;
var green:uint = hexColor >>> 8 & 0xFF;
var blue:uint = hexColor & 0xFF;

Timestamp in AS3

I needed a Unix style timestamp to insure a unique number each time and here it is:

var now:Date = new Date();
var timestamp:String = now.valueOf().toString();

valueOf() returns the number of milliseconds since midnight January 1, 1970.