How to make a class or package import optional at compile time
Lets say you are using a package for debugging and want an easy way to turn it on and off. The idea behind it is that turning it off would also remove the import for obvious fill size reasons.
This handy solution is made possible constants. If you define a constants. If you define a constant, for instance CONFIG::DEBUG, to a Boolean value you can turn a block of code on and off from the compilation point of view.
In Flash IDE, you can set those constants in PUBLISH SETTINGS> “Settings…”>”Config constants”. For other editor you can set the flex compiler command line options as follows:
-define=CONFIG::DEBUG,true .
I have a package level function, called log(), that I use in place of trace() to output in the JavaScript console. Here is how I use the compiler constant to turn that feature on and off.
public function log(…args):void
{
CONFIG::DEBUG
{
[callthe debug package and pass the arguments]
When CONFIG::DEBUG is set true, the log is calling the debugging package. Otherwise, the compiler ignores the entire block. Log() turns into an empty method. All references to my debugging stuff are removed, therefore not imported.
All I have to do so is change one constant and all my log() methods are disabled.
Squeeze for speed
So you’ve spent all that time optimizing your assets , have you considered your data?
While file compression is nothing new, here’s simple solution you may have overlooked. As sites become more complex and data driven, users expect content to download at faster and faster rates, and we often find ourselves trying to optimize our work as much as possible. One area that focused on is the way we load data into our Flash sites.
XML is by far and away the ‘standard ‘ in regards to maintaining a dynamic Flash website. We figured out that I could optimize the size of XML files being loaded into Flash by compressing them. In order to do this, we turned to GZIP compression and the ability of Flash to read binary data. Since then, we have made it a standard to incorporate GZIP compression into my workflow when involving XML data, where the resulting compressed XML file is 85-90 percent smaller than the uncompressed original.
My process works as follows:
- We choose the desired XML file we want to compress and run it through the GZIP compressor. The output is saved as a binary GZ file.(You can download the GZIP compressor at http://www.gzip.org ). Compression can be done via a manual process or an automated (for example via a CMS).
- In Flash, I load the.gz filevia URLLoader, making sure I set the URLLoader’s dataFormat property to binary.
- When loaded, we save the data as a ByteArray ,uncompress it, and reset its position property to 0;
- Using the method readUTFBytes on the byteArray object and passing the property bytesAvailable, we convert the loaded binary file into a String which in essence is the XML we are looking for.
- The last step is crating my XML object, which is created by passing my converted string into the constructor.

ActionScript 3.0 in Flex Builder Essential Training



