my top 5 actionscript3 tips

1. Having problems viewing the correct fonts in your flash file? I have found this selection of code really useful in outputting what has been embedded into flash, meaning it doesnt depend on the client having that font.

var embeddedFonts:Array = Font.enumerateFonts(false);
for(var i:Number = 0; i < embeddedFonts.length; i++){
var item:Font = embeddedFonts[i];
trace("[" + i + "] name:" + item.fontName + ", style: " + item.fontStyle + ", type: " + item.fontType);

2. Passing values to event listeners, a simple idea but has caused me many problems whilst learning actionscript 3. The simplest way i have found is to use the name parameter. In this example the loader loads a image file and on completion calls the subfunction in here we access the loader and trace out its name. This name variable could be used for other things to index the loader if you are loading multiple files using the same onComplete function.

var loader:Loader = new Loader(); = "myparam";
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, subfunction, false, 0, true);
loader.load(new URLRequest(""));
function subfunction(e:Event):void {
var ldr:Loader = e.currentTarget.loader as Loader;
trace(; //outputs myparam

3. Single function for event listeners to keep your code more compact and easier to read try using the same function for your event listeners and a switch statement for the type.

btn.addEventListener (MouseEvent.MOUSE_OVER, _handleMouseEvent);
btn.addEventListener (MouseEvent.MOUSE_OUT, _handleMouseEvent);
btn.addEventListener (MouseEvent.CLICK, _handleMouseEvent);
function _handleMouseEvent( evt : MouseEvent ):void
switch (evt.type) {
case "mouseOver":'in');
case "mouseOut":'out');
case "click":

4. Flash memory useage is very important and one of the easiest ways to monitor this is to use the code snippet below. (note works best if any other flash player instances are closed even those embedded in any web pages you have open)

var mem:String = Number( System.totalMemory / 1024 / 1024 ).toFixed( 2 ) + 'Mb';

5. Center aligning display objects on stage can depend on the registration point of the displayObject typically my registration point is either the center middle or top left.

//for center registration point
dObj.x = stage.stageWidth/2;
dObj.y = stage.stageHeight/2;
//for top left registration point
dObj.x = (stage.stageWidth/2) - (dObj.width/2);
dObj.y = (stage.stageHeight/2) - (dObj.height/2);


    Comments are currently closed