If you want to write a unit test which verifies that something implements a particular interface, or extends a particular class, here’s how…
A couple of years ago, I created an object pooling utility for a games project I was building in AS3. Since then, I’ve used it quite a few times, in order to speed up apps and improve resource management, easing the load on the garbage collector by reusing objects instead of recreating them.
While object pooling isn’t a magic bullet to speed up every use case, it works especially well on things that are heavy to continually construct and destroy. A good example is my History of the World project, which uses an object pool for item renderers, instead of creating and destroying them as you navigate around – press ALT+CTRL to bring up the resource debugger, which shows a little information on its usage.
I recently updated the utility, improving its performance, adding features and putting loads of unit tests around it. It’s now hosted it over at GitHub. Using it is a simple as:
var pool:LoanShark = new LoanShark(SomeClass);
var someInstance:SomeClass = pool.borrowObject();
// Instead of nullifying an object, check it back into the pool
If you’re still churning out Flash banners, please use this!
I created this simple utility, called SWFIdle, to enable the Flash Player to lower its CPU usage while the user is not interacting with it. Since it’s possible to have multiple Flash instances embedded in one page (for example, a game and a couple of banners), I recommend that everyone uses this in their projects, so that players needn’t fight for CPU and give a worse name than it has already.
I know there’s the hasPriority embed attribute now. But:
- That assumes you have access to the HTML that embeds your SWF
- If no other players are present, it has no effect
- There’s still usually little reason to be running your SWF at a high framerate if the user isn’t interacting with it
- Flash banners with wastefully unoptimised drawing routines are probably one of the key reasons that Flash got poo-pooed off of mobile platforms and disabled on everyone’s laptops – CPU usage = battery usage!
You want to embed an entire SWF, full of assets, into a class and retrieve individual symbols from it – but you don’t want to have to embed each asset individually.
SWFAsset – a couple of lines of code and you’re away.
You’re using Flash 10’s native 3-D API and notice the projection goes a little skewiff when resizing the window.
You need to reset the stage’s projection centre on stage resize, like so…
var centre:Point = new Point(stage.stageWidth/2, stage.stageHeight/2);
root.transform.perspectiveProjection.projectionCenter = centre;
In moving to compiling projects with the new Flex SDK 4, I noticed a couple of gotchas to do with the EMBED metatag that I thought I’d share:
Runtime Shared Libraries
If you wish to embed assets in your SWF with the EMBED metatag, so you can manage and update things easily, there’s an extra compiler parameter you must add, in order for your project to compile properly:
This is already added as a new default parameter in FlashDevelop projects. But if you’re planning to build projects from outside a similar IDE, you must add this to your compiler string. Otherwise, the compiler will think you have uninitialised constants and warn you so.
Using the EMBED metatag, or even better runtime loading, for fonts is the sensible way forward. The amount of projects I’ve seen where you need to build from an FLA file full of fonts, which you need to hunt down and install is crazy. With Flex SDK 4, you’ll need to add an extra attribute to your embed tag for fonts, called ’embedAsCFF’:
[Embed(source=’myfont.ttf’, fontName=’MY_FONT’, fontWeight=’regular’, unicodeRange=’U+0020-U+0040,U+0041-U+005A’, mimeType=’application/x-font’, embedAsCFF=’false’)]
public static const MY_FONT :Class;
With my simple FlashSize script, all you need do is call:
Update: This project is now hosted at GoogleCode
I recently stumbled over a new AS3 gotcha that seems to be bugging a lot of the Flash community. Apparently a new ‘feature’ of AS3, whereby using the mousewheel inside an embedded Flash movie still scrolls the surrounding browser window (if it has enough content, that is).
This didn’t happen in AS2. If a Flash movie had focus, mousewheel events weren’t sent to the browser window. It wasn’t a problem in FireFox either, until a recent update and adversely affects any AS3 Flash app embedded in an HTML page where you might want to scroll or zoom with the mousewheel while over the Flash, instead of the scrolling the browser window.
Setting up MouseWheelTrap is easy:
Simple huh? Let me know how you get on…