Flash optimisation
I’ve spent some time designing and developing in Flash but I’m never happy unless I’m learning faster and more efficient ways to develop my projects. A key is to develop faster applications that run smoothly on any platform and use a minimal about of system resources, animation that is quickly and smooth as well as develop cleaner and more user friendly Actionscript code.
I’ve come across several blogs and website’s which have tips and tricks on how to optimise your Flash projects, so I thought I would consolidate them in the post. They are from various sources such as:
Eight code optimisation tweaks:
- Declare the types of all variables
- Do not use array length inside loops or in loop headers
- Avoid multiple array lookups
- Use int for counters and array lookups (AS3 only)
- Use Number for math operations. There are cases where int is faster but it is generally best to stick with Number (AS3 only)
- Use multiplication instead of division
- Access variables directly, i.e without using getter and setter functions
- Declare many variables at once
Two AS3 implementations of the same loop
//Slow unoptimized loop:
for(var i = 0; i < arrayOne.length; i++){
if(arrayOne[i].enabled()){
arrayOne[i].getX() = arrayTwo[i].getX()/2;
arrayOne[i].getY() = arrayTwo[i].getY()/2;
arrayOne[i].getZ() = arrayTwo[i].getZ()/2;
}
}
//Fast optimized loop:
var length:int = arrayOne.length, i:int=0;
for(; i < length; i++){
objectOne:SomeObject = arrayOne[i];
objectTwo:SomeObject = arrayTwo[i];
if(objectOne._enabled){
objectOne._x = objectTwo._x*0.5;
objectOne._y = objectTwo._y*0.5;
objectOne._z = objectTwo._z*0.5;
}
}
Design / Develop tips
Tip 1. Avoid bitmaps with alpha channel whenever possible. When two bitmaps containing an alpha channel are stacked on the stage, Flash has to do extra calculations to figure out the color of the pixel on every pixel where the bitmap contains alpha information. That adds a lot of computing.
Tip 2. Always draw/attach only those elements that are visible on the screen at the given moment, reuse movie clips whenever possible and try to control the number of clips on stage - the fewer the better.
Tip 3. Try to have as many graphical and textual assets imported to your FLA, and load externally at runtime only what is absolutely necessary.
Tip 4. Flatten graphical assets to minimize their number. If you have a button that is composed of a background, a shadow, an icon and a text label, the ideal solution is to export it from Photoshop as one flattened bitmap with all those elements in it (yes, even the text label!).
Tip 5. Optimize bitmaps in Photoshop not in Flash; the Photoshop save-for-web module is a lot better than Flash when it comes to optimizing bitmap files. In the FLA file, all the imported bitmaps should then have the lossless PNG/GIF compression option checked - Flash does not need to compress them any more.
Tip 6. The less ActionScript the better. Always minimize the amount of code you write. Especially do not create functionality that “will be useful in the future”. In the future you almost always need something else that you prepared for anyway. Also, AS is for interactivity, not for building stuff (the stage is designed for that). So if half of your code consists of attachMovie calls, something is wrong.