ActionScript 2 variable scoping in CS4 / FP10

Over the last month I’ve been working on a legacy project which was written in ActionScript 2 and is published as an EXE using MDM Zinc (2.5).

ActionScript 2 has always been fairly poor at variable scoping, the code I was working with did some questionable things with variable scoping, For Example

if (a==b)
var the Frame=2;
var theFrame=3;

This sort of code would not work in Java or C++ etc, because the variable theFrame is declared in the scope of the true or false parts of the “if” statement. However AS2 normally propagates variable scoping outwards as well as inwards, in these cases.

In this particular situation, the code failed to work when compiled to AS2 from CS4, and when using Zinc v2.5. The client said that the resulting bug  had not been seen before., so I have to assume its was triggered by either using CS4 rather than an older version of Flash e.g. Flash 8, or by Zinc using version of the Flash player installed on my machine i.e. FP10. Either way its a reminder to think carefully about variable scoping, even in AS2.

While I’m on the same subject. I  also had an instance where a reference  to variable used in an inline function failed also to be in-scope.


function setupHandlers()
var myVar:Number=5;
someMC.onRelease = function()

Normally this would work in AS2, as the onRelease function holds a reference to myVar, however in my build / run enviroment this also stopped working. My work-around was to make use of the fact that all AS2 classes are “dynamic” i.e. can be added to, so I added a myVar property to the someMC MovieClip and used the myVar property of the MC inside the inline function.