Monday, March 10, 2008

Migration from AS2 to AS3: Alpha values

If you've been migrating legacy code to AS3 for a while, this is very old news.

Migrating old code from AS2 to AS3, can be an adventure in tedium. It's easy to spot "._x" and convert to ".x", and replace "createEmptyMovieClip" with new MovieClip, but less obvious to spot are the alpha values.

In AS2, you could write
b._alpha = 40;
And this would set the alpha of clip "b" at 40 percent.

To do this in AS3, you need to write
b.alpha = .4;
because, in AS3 100 percent is indicated as 1.

It's especially easy to miss, at first go, if you're migrating a color transformation. Here's an example of a transformation I was migrating today.

In AS2, it looked like this:

var b3 = targ.createEmptyMovieClip("bubble3", targ.getNextHighestDepth());

var fillType = "radial"
var colors = [0xFF0000, 0x0000FF];
var alphas = [20, 20];
var ratios = [10, 200];
var spreadMethod = "reflect";
var interpolationMethod = "RGB";
var focalPointRatio = 0.1;
var matrix = new Matrix();
matrix.createGradientBox(100, 100, 0, 0, 0);

b3.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio);

. . .

In the AS3 vesrion, there are a several changes. It would be easy to miss the change in scale for the values held in the array "alphas".

var b3 = new Sprite();

var fillType = "radial"
var colors = [0xFF0000, 0x0000FF];
var alphas = [.2, .2];
var ratios = [10, 200];
var spreadMethod = "reflect";
var interpolationMethod = "RGB";
var focalPointRatio = 0.1;
var matrix = new Matrix();
matrix.createGradientBox(100, 100, 0, 0, 0);

b3.graphics.beginGradientFill(fillType, colors, alphas, ratios, matrix, spreadMethod, interpolationMethod, focalPointRatio);

. . .

this.addChild(b3);
Oh, and don't forget to add your new display object. I'm sure there are other things I've missed, but this covers the new display model in AS3 and the alpha values.

0 comments: