Projects

This is the list of all Actionscript classes I’ve been working. These are classes and extensions that I use on my day to day work, and they might also be useful for someone else.

Random disclaimer: please notice that all the content available here is provided as-is. Supplied code is free to use but without the guarantee of support of any kind.

Please notice: Unless otherwise stated, these classes are freely available from my Subversion repository, located here. And while you can download the files directly using your web browser, I would recommend using some subversion client instead. TortoiseSVN is generally considered a good Subversion client; there’s a few nice tutorials about it here. This might seem complicated at first, but subversion is the easiest and safest way to have update source code delivered to your folder of choice.

Fnk

Fnk is an online visual programming environment that uses a dataflow approach for the analysis, processing and synthesis of image and sound in real-time. It’s my main bachelor graduation thesis (under the Digital Interface Design course at Senac), and still under construction, due only on dec’08.

Visit the Fnk Website


caurina.transitions.Tweener (AS2 and AS3)

Tweener is an animation and transition class. It’s what I use for creating dynamic animations via code, like button rollover states, fades, and other visual effects (and to avoid using the timeline for just about anything). And, although it obeys a slightly different syntax philosophy, it’s the spiritual successor to my MC Tween.

Download: Tweener is hosted on Google Code, so one can find documentation and download information there.


zeh.compression.LZW (AS2)

LZW is a static class that allows string compression using the Lempel-Ziv-Welch – or LZW – algorithm. It provides good compression with a fast engine that can have split iterations and is ideal to send large amounts of compressible data to a server, like the points that form an image.

How to use:

import zeh.compression.LZW;
var myString = "This is a test string";
var myCompressedString = LZW.compress(myString);

Sometimes, however, big strings might take too long to compress, and this can’t be done at once because it could halt the player execution and could potentially lead to displaying the “This script is taking too long” warning, which allows the user to cancel script execution.

To avoid this, the LZW class allows compression to be split between frames and be executed in the background. It’s done like this:

import zeh.compression.LZW;
var myString = "This is a test string";
var myID = LZW.slowCompress(myString);

This starts the string compression in the background. Of course this will slow down Flash execution due to the complexity of the algorithm (it compresses 1000 chars per frame); however, it will avoid freezing the movie altogether.

After the compression has started, you can get the percentage of completion by using the getSlowCompressFactor() function. For example:

trace ("The string compression is " + (LZW.getSlowCompressFactor(myID) * 100) + "% complete.");

When this value reaches 1, it has completed. You can then get the compressed data:

myCompressedString = LZW.getSlowCompressString(myID);
LZW.dispose(myID); // delete the string compression data to clean up memory

If you need it, the decompression works in the same way, either with a quick decompression…:

var myDecompressedString = LZW.decompress(myCompressedString);

…or with a background decompression:

var myID2 = LZW.slowDecompress(myCompressedString);
...
trace ("The string decompression is " + (LZW.getSlowDecompressFactor(myID2) * 100) + "% complete.");
...
myDecompressedString = LZW.getSlowDecompressString(myID2);
LZW.dispose(myID2); // delete the string and its data to clean up memory

Compressed strings can then be sent to server-side scripts just as they where normal strings, using LoadVars or similar interfaces. After that is done, the data sent to the server would then have to be decoded by a server script who recognizes LZW data streams.

References used in the creation of this class are the Wikipedia entry for LZW, including parts of the Python code, and this article on LZW compression.

As a last note, keep in mind that not all tasks that involve transmitting huge chunks of data are better solved with LZW compressing. Depending on the kind of data you’re working with, RLE compression might be easier to implement on both sides and might be as good as LZW would; other times, you might not even need compression because the TCP/IP protocol already compresses data on the fly and the time spent with LZW compression would actually be greater than the time saved when uploading the data. Deciding which approach is the correct one on each specific case is an important step that must not be overlooked; LZW will come in handy sometimes, but due to Actionscript 2′s lack of execution speed, it’s not a definitive solution that can be applied on all cases.

Download: LZW is available on the subversion repository.


zeh.loading.ImageLoader (AS2)

ImageLoader is a straightforward image loader. It simplifies the job of loading images by automatically creating a list of images being loaded, controlling its queue with priority features (by way of the LoadingQueue class), and by caching images locally using BitmapData instances. This means that when you try to load a new image, it actually loads the image, saves its BitmapData, and attaches the image to the container (with smooth turned on by default). Additional image loadings will simply be skipped and the cached bitmap is used instead. Simultaneous downloads also “understand” each other so if you’re loading the same image on two different containers at the same time, they will have the correct loading percent set and events fired.

How to use:

import zeh.loading.ImageLoader;

var myImgLoader:ImageLoader = new ImageLoader(whateverMC);
myImgLoader.onStart = function() {
	trace ("loading is started");
};
myImgLoader.onUpdate = function(f:Number) {
	trace ("loading is " + (f*100) + " percent complete");
};
myImgLoader.onComplete = function() {
	trace ("loading has finished");
};
myImgLoader.loadImage("whatever.jpg");

To control priority, use the second parameter:

var loader1:ImageLoader = new ImageLoader(whateverMC1);
loader1.loadImage("whatever1.jpg", 5);
var loader2:ImageLoader = new ImageLoader(whateverMC2);
loader2.loadImage("whatever2.jpg", 0);

On the above case, in case there’s a queue of different loadings taking place, loader2 will load before loader1, because the second parameter (priority) is lower.

Priority is just a comparison number; two imageloaders can have the same priority (the first one loads first). Default priority is 5.

Images are attached to the target MovieClip with smoothing set to on. If you don’t want the loaded image to be smoothed, however, just specify the third parameter of the loadImage() method as being false:

var iLoader:ImageLoader = new ImageLoader(whateverMC1);
iLoader.loadImage("whatever2.jpg", undefined, false);

Using true as the third parameter will use smoothing instead. But since this is the default setting, it’s not needed.

It’s important to notice that ImageLoader makes use of two loading queue slots by default; that way, no more than 2 loadings take place at the same time globally. This can be changed by using the static setSlots method at any time:

ImageLoader.setSlots(2);

Dependencies: ImageLoader also needs zeh.loading.LoadingQueue present.

Download: ImageLoader is available on the subversion repository.


zeh.loading.LoadingQueue (AS2)

LoadingQueue is a simple loading proxy function that controls queuing of loading actions, so you can better control what gets loaded, when, in what order, without being forced to call a bunch of loads and pray that they don’t timeout in case there are too many files to load.

How to use:

import zeh.loading.LoadingQueue;

var lq:LoadingQueue = new LoadingQueue();
lq.addMovie(myMovie, "picture.jpg");
lq.addXML(myXML, "data.xml");
lq.start();

The above code creates a new simple queue and then start loading the content on the containers.

New LoadingQueue instances take a parameter that refers to the group that loading queue takes part. This is unrelated to the loading scope. For example:

// Creates two different loading queues
var lq1:LoadingQueue = new LoadingQueue();
var lq2:LoadingQueue = new LoadingQueue(this);

// Creates two different loading queues but that act as one loading queue only
var lq1:LoadingQueue = new LoadingQueue();
var lq2:LoadingQueue = new LoadingQueue();

Each queue has also a number of maximum loading slots. The default number of slots is 1 per queue. You can change it when creating new loadings:

var lq:LoadingQueue = new LoadingQueue(this, 3);

That way, 3 loadings will take place simultaneously. This can also be changed after creating your loading queue instances by using the setSlots method:

lq.setSlots(2);

Existing loadings are finished even if they take more slots than the new value (they are not cancelled); but new loadings won’t start until there are available slots on that queue.

Finally, you can also use priority when adding containers to the loading queue:

lq.addMovie(myMovie1, "a.jpg", 5);
lq.addMovie(myMovie2, "b.jpg", 0);

Or:

lq.addXML(myXML, "data.xml", 5);
lq.addXML(myXML2, "data2.xml", 0);

On the first example, in case there’s a queue, myMovie2 will be take priority over myMovie1 because its priority number (0) is lower. The same applies to the XML example. The default priority is 5.

Please notice that this class is not meant to replace the normal preloading procedures one might use when loading content into a Flash movie. It is, instead, meant to juggle the process of triggering loadings according to some specific needs, using priorities and a defined number of slots; you can then use whichever solution you want to measure the progress of the loading procedure inside your content.

Download: LoadingQueue is available on the subversion repository.