For some Flash developers file size isn't really an issue.  Obviously we don't want to be wasteful of resources when creating an application, but in general, it may not be something we think too much about.  However, there are times when a small swf file is absolutely necessary.  I've been working with strict size limitations for the last few months which mandate that swf files be no larger than 40KB.  I'm not necessarily the one who creates the fla files to begin with, but it has become my task to size them down and I've found some pretty neat tricks for cutting back on file size that perhaps you may find useful as well.  Recently I was handed a 62KB file and was able to whittle it down to 36KB using the techniques discussed in this article!

Before we jump into the specific methods for cutting back on file size, we need to make sure you can see the results for yourself.  In order to do this, you'll want Flash to generate a size report for you.  The size reports that Flash creates show you how much space individual items are taking up.  Analyzing the size report will allow you to identify areas that may benefit the most from a little optimization.  To see the size report for a file, go to File >> Publish Settings.  Click on the Flash tab and under the Advanced section you will see a Generate size report checkbox; we want to make sure this box is checked!  Now when you publish the file, Flash will display a size report in the Output panel.  It will also create a text file in the same directory as the fla.  Now that we've set up Flash to give us a report, let's take a look at some techniques for shrinking your swf files.

One of the best ways to cut back on your file size is to analyze the fonts you are using.  If you're animating some text or adding effects, you're going to want to embed the font so that it shows up and looks nice, but be careful!  Embedding a font can bloat the size of your file tremendously!  Many times I have worked with Flash files where the creator simply did a blanket embed on a textfield; this is incredibly wasteful as you are including many glyphs that will not be used!  Instead, take the time to selectively embed only the characters that are likely to be used.  

Consider this: embedding lowercase letters, uppercase letters, numerals and punctuation in the standard Arial font will eat up 13KB all by itself!  You can shave that down to around 10KB by embedding only the most commonly used glyphs.  That's certainly an improvement, but 10KB is still pretty large for just one font.  If the font you are using is taking up too much space, you can try to find a similar looking font that may be smaller in size.

It is also worth noting that if you have one regular textfield and another that is bolded, you are now looking at embedding two font styles!  With the Arial example, this means the fonts are now eating up 20KB of space and when you're working with a strict 40KB limit, that is simply not acceptable.  Bolded text is meant to stand out, but clearly, we cannot afford to embed both the regular and bold styles of the font.  One trick I often use in this situation is to make the text stand out using some other method; a nice alternative to bolding is to use a strong glow filter with minimal blur.  This allows the text to grab a user's attention without bloating your file size.  The following image shows this technique in action; the top most line is bold text, the bottom line is regular and the middle line is using a glow filter with an x/y blur of 1, a strength of 200 and the quality is set to high. 

Optimizing images goes a long way in reducing swf file size.  A good artist will optimize the images before they even get into Flash, but once there, we can cut back the size even further.  Once an image is in the library, right click on it and select properties.  Notice the dropdown next to Compression; it gives us a couple options: Photo or Lossless.  By selecting Photo, we can then click the Custom radio button and specify the quality we want to use for this image.  Once you enter a quality, click the Test button and Flash will show you how the photo will appear and also give you a summary of the size savings.  Generally I find that I can set image quality to around 75 and get some nice savings without sacrificing much visual quality.  If you go much lower than that, the image degradation can start to become really noticeable.

Another good way to save space on images is to get rid of any unnecessary ones!  This may sound like common sense, but many times I have seen people use two different images for left and right navigation arrows when really they could just use one image and rotate it.  Also, if it is only the initial file size you are concerned about, consider loading other images and assets at runtime to keep the swf size lower.

Vector images or shapes deserve a special mention.  I was able to cut out almost 5KB by simply eliminating portions of oversized vector shapes within an fla file.  There were two specialized masks that had been created and when I turned off the masking to see the shapes, I could see that they were far larger than they needed to be!  The shapes extended beyond the stage boundaries so I simply cut off the portions that were not going to be displayed and came up with substantial savings in terms of file size.

For many people, myself included, this may be the hardest area to realize big savings with.  However, there are ways to cut back on wasteful ActionScript if necessary!  One of the most obvious ways to save space with code is using a third party tween engine.  I use several different engines and find most of them pretty handy, but when it comes to pure size, you'd be hard pressed to find something better than GreenSock's TweenNano class.  At just 1.6k, this tween engine is both tiny and functional.  For simple animations, it is more than capable of getting the job done.  The GreenSock Tween Engine is available for free to most developers and for others it has a very reasonable licensing model.  If you don't want to foot the bill yourself, see if you can get your company to buy you a license.  Once you show them the size savings and performance you may find it to be an easy sell.

Another good way to cut back on size with code is to ditch some of your utility classes you may be accustomed to using.  I know, they can be really handy, but it won't kill you to do things the old fashioned way once in a while.  Many times utility classes simply wrap common functionality into an easy-to-use package, but by doing so they introduce extra overhead into your program and in some cases, that extra overhead may be wasting precious bytes that you need to take back!  Don't be afraid to dump that XML loading class you created in favor of the straight URLRequest and URLLoader it likely uses anyway!

By utilizing the techniques described above, you can get some noticeable improvements in the file size of your swf's.  I hope you found this information useful; please share any additional tips you may have that were not covered here!