While refactoring the reflection part of the as2lib I came across an issue related to cross-referencing of static vars in Flash. Suddenly one of my static vars was undefined and I couldn’t guess why. I have minimized the code to only the core parts but it nevertheless enfolds three classes. Thus read on to hear what went wrong.
Like I said, I had three classes. These classes were cross-referencing each other (after a few refactorings). Here they are:
-
class FirstClass {
-
private static var traceClassName = trace ("FirstClass\n");
-
private static var traceStartedInitializing = trace (" > started initializing static vars");
-
-
private static var fcVar:String = "FirstClassVar";
-
private static var scVar:String = new ThirdClass().getScVar();
-
-
private static var traceFinishedInitializing = trace (" > finished initializing static vars\n");
-
private static var border = trace ("—————————————–\n");
-
-
public static function getFcVar(Void):String {
-
return fcVar;
-
}
-
-
public static function getScVar(Void):String {
-
return scVar;
-
}
-
}
-
-
class SecondClass {
-
private static var traceClassName = trace ("SecondClass\n");
-
private static var traceStartedInitializing = trace (" > started initializing static vars");
-
-
private static var scVar:String = "SecondClassVar";
-
private static var fcVar:String = FirstClass.getFcVar();
-
-
private static var traceFinishedInitializing = trace (" > finished initializing static vars\n");
-
private static var border = trace ("—————————————–\n");
-
-
public static function getScVar(Void):String {
-
return scVar;
-
}
-
-
public static function getFcVar(Void):String {
-
return fcVar;
-
}
-
}
-
-
class ThirdClass {
-
private var scVar:String;
-
-
public function ThirdClass(Void) {
-
trace ("");
-
trace (" ThirdClass\n");
-
trace (" > started instantiating");
-
-
scVar = SecondClass.getScVar();
-
-
trace (" > finished instantiating");
-
trace ("");
-
}
-
-
public function getScVar(Void):String {
-
return scVar;
-
}
-
}
As you can see the FirstClass obtains a reference to the scVar of the SecondClass over the ThirdClass. The SecondClass has a reference to the fcVar of the FirstClass. If you now make simple test that looks like this:
-
trace ("FirstClass\n");
-
trace (" > " + FirstClass.getFcVar());
-
trace (" > " + FirstClass.getScVar());
-
-
trace ("");
-
-
trace ("SecondClass\n");
-
trace (" > " + SecondClass.getScVar());
-
trace (" > " + SecondClass.getFcVar());
You will get the following output:
-
SecondClass
-
-
> started initializing static vars
-
> finished initializing static vars
-
-
—————————————–
-
-
FirstClass
-
-
> started initializing static vars
-
-
ThirdClass
-
-
> started instantiating
-
> finished instantiating
-
-
> finished initializing static vars
-
-
—————————————–
-
-
FirstClass
-
-
> FirstClassVar
-
> SecondClassVar
-
-
SecondClass
-
-
> SecondClassVar
-
> undefined
As you can see the SecondClass’s static vars get initialized before the FirstClass’s static vars and the reference to the fcVar from the SecondClass to the FirstClass is thus undefined.
If the FistClass would obtain the scVar from the SecondClass directly via: SecondClass.getScVar() the compiler would cause the following Error:
-
**Error** Scene=Scene 1, layer=Layer 1, frame=1:Runtime circularities were discovered between the following classes:
-
FirstClass
-
SecondClass
I would have loved to get this Error message too. It would probably had saved a lot of time. Hopefully you will now no what the problem could be if you encounter a similar problem in your system.
