Actionscript “Gotchas”

I’ve learned a few of these during my recent battles with Flash and Actionscript & wanted to document them for

1) Unlike Javascript, you can’t call functions until they are defined. Flash will not complain about this at compile time nor at runtime however. You can call undefined functions all you want – it’s just a no-op to Flash.

2) Beware of loose typing. When pulling data from xml, for example, you must explicitly cast to the correct datatype. Note that you can compare Strings but you’ll get unexpected results.

3) Sometimes you can get into race conditions due to animation. For example, I have a method called “resize_bar” that sets a colored bar between two date range sliders. When the date sliders are moved, this method gets called to adjust the size of the bar, but it has to be called several times in rapid succession to follow the animation. This could be solved by simply calling the method in a loop with setInterval, but that puts unnecessary load on the CPU. So here’s the solution I came up with; it works nicely but requires some trial and error to get the values just right.


var displayBoxesReps;
var displayBoxesInterval;

// need to do this several times in rapid succession to handle the animation.
// we don't want to keep doing it forever though...
resize_display_boxes = function () {
displayBoxesReps = 1000;
displayBoxesInterval = setInterval(doResizeDisplayBoxes, 200);
};

doResizeDisplayBoxes = function () {
if (displayBoxesReps==0) {
clearInterval(displayBoxesInterval);
} else {
// set some positions and format for the date range readout things
readoutmin_mc.readoutmin_txt.autoSize = "right";
readoutmin_mc.left_mc._width = readoutmin_mc.readoutmin_txt._width-15;
readoutmax_mc.readoutmax_txt.autoSize = "left";
readoutmax_mc.right_mc._width = readoutmax_mc.readoutmax_txt._width-17;
// set the box position and width to the slider handles
box_mc._x = sliderone_mc._x+(sliderone_mc._width/2);
box_mc._width = (slidertwo_mc._x)-(sliderone_mc._x);
//trace("in resize_display_boxes, displayBoxesReps is " + displayBoxesReps + ", box_mc._width is " + box_mc._width);
displayBoxesReps--;
}
};

Leave a Reply

Your email address will not be published. Required fields are marked *