Archive for September 2009


Building a Preloader with ActionScript 3 using mxmlc and the Frame Pragma

23. September 2009 - 13:56 Uhr

Good news: you really don’t need the timeline to build a preloader. The technique is described in detail at bit-101. In short it works like this:

1. put this before the definition of your main class:

[Frame(factoryClass="myPackage.MyPreloadClass")]

The code above tells the mxmlc to take the given (custom) class under myPackage.MyPreloadClass and use it as a Preloader (there is some more background info about the Frame-pragma on the adobe-blog).

2. Write your preloader-class, extending the MovieClip-Class (since you need something with frames and sprites don’t have them). See bit-101 for an example-class.

3. That’s it. Er … well, not completely, since if you need to load some other stuff, say for example depending on a CMS-generated XML-File, the path to which is handed to your swf using flashvars, you will no longer get the the flashvars with

LoaderInfo(mainClass.root.loaderInfo).parameters

but with

LoaderInfo(preloader.root.loaderInfo).parameters

where “preloader” is a reference to the preloader-instance – that’s why I am passing a reference to the Preloader as a parameter when instantiating my main class.

5. Now your preloader would still think everything is ready, once the swf is completely loaded. That’s not the case, since you still lack the stuff defined by your on-the-fly-cms-generated XML.

My solution is to pass my main class a callback-function as another parameter, which tells the preloader when the loading is really done.

In principle the passing of a preloader-reference to the main class would suffice, but then the function triggering the end of the loader-animation would have to be defined public …

Kommentieren » | digital

Manipulating Bitmap Images with ActionScript 3

8. September 2009 - 19:40 Uhr

I’ve been recently working on a project which involved the manipulation of bitmap data at runtime. The swf would be communicating with a content management system, which only provided coloured images. But since the images needed to be displayed in greyscale as well as in colour, the swf had to do the trick.

In ActionScript you need to use the ColorMatrixFilter to manipulate these properties. A procedure which can be mind boggling and definitely is error-prone.

After a little searching I found the extremely handy class ColorMatrix.as written by ActionScript grandmaster Grant Skinner. You’ll find some examples on his blog and the class itself hosted on google code.

The usage is fairly simple, though you need to take care of the order of the function calls:

import com.gskinner.geom.ColorMatrix;
import flash.filters.ColorMatrixFilter;
import flash.display.Sprite;

/* ... some other code in between ...*/

var c:ColorMatrix = new ColorMatrix();
c.adjustBrightness(100);
c.adjustSaturation(-100);
c.adjustContrast(-50);

var cmf:ColorMatrixFilter = new ColorMatrixFilter();
cmf.matrix = cm;

// The following bitmapData is the BitmapData-Object
/  derived from an imagefile,
// which is loaded at runtime:

var bmp:Bitmap =  new Bitmap(bitmapData) ;

var s:Sprite = new Sprite();
s.addChild( bmp );
s.filters=[cmf];

// that's it.

It’s a breeze and works like a charm. Just keep in mind, that these lines:

c.adjustBrightness(100);
c.adjustSaturation(-100);
c.adjustContrast(-50);

will result in a different image compared to the product of these lines:

c.adjustSaturation(-100);
c.adjustContrast(-50);
c.adjustBrightness(100);

Kommentieren » | digital