You need this like a hole in the head

Month: November 2010

Flash AS3: Track SWF memory usage and framerate with SWFProfiler

This is just a quick post to let you know about another essential AS3 class you should download right now. It’s called SWFProfiler and it adds this slick memory usage and framerate monitor to your SWF:

SWFProfiler Shane McCartney

Shane McCartney has built this clever component and released it for free. It is completely self-contained in it’s own class file. So, using it only requires 2 lines of code:

import com.lia.utils.SWFProfiler;
SWFProfiler.init(stage, this);

I should also mention that it is automatically added to the context menu of your SWF, so you just right-click to show it or hide it.

To see the SWFProfiler in action, go to Shane’s blog.

You can download the most recent version of the SWFProfiler class from google code. Shane has a few other goodies there as well.

Flash AS3: check if an AS3 method exists

If you build in Flash, you know that each new release of Flash will have new features in Actionscript3. For example, there is a bug in Flash 9 (CS3) with the Loader.unload() method that can cause it to hang on to the loaded item in memory (details here). This was fixed (sort of) in Flash 10 with the Loader.unloadAndStop() method. There are also other features, such as byteArrays that weren’t available in Flash 9, but were added in Flash 10.

At my job, we have created a class package for commonly used functionality, such as asset loading. But what if we want to use a method like Loader.unloadAndStop() in one of these classes? If the project is published in Flash 9, the compiler will throw an error. We could simply use the Loader.unload() method, but we really want to use newer method if the project is published in Flash 10. As always, the Flash community comes to my rescue with this:

var methodExist:Boolean;
try { //FP10 and up:
	methodExist = this["unloadAndStop"] != null;
} 
catch (e:Error) { //FP9:
	methodExist = this["unload"] != null;
} 

This handy little snippet of code will work in any version of AS3. It looks for the newer function and runs it if it exists. If it doesn’t exist, it reverts back to the older code. This is a lifesaver. It allows me to run the “best” version of Actionscript, based on the publish settings of the project.

The original forum thread where I found this solution is here.

Powered by WordPress & Theme by Anders Norén