Reputation: 1627
I'm sorry, this is probably a bit of a daft question, but I'm trying to write an Actionscript 2.0 class that handles loading XML data and then makes it accessible to the rest of the application.
However, this is the first time that I've written a class definition for anything that wasn't a MovieClip, and I'm running into a strange error. For instance here:
class Main extends SEBanner {
public function Main() {
_root._quality = 'BEST';
trace(_root._quality + ' SWF Quality Setting.');
trace('Banner main object instantiated. Ready to run.')
trace('Width of banner: ' + bannerWidth());
trace('Height of banner: ' + bannerHeight());
trace('Banner aspect ratio: ' + bannerAspectRatio());
trace('----------------------------------------------');
}
public var externals:External = new External();
}
the last line, where I try to instantiate the External class (named so because it will handle externally-provided Flash variables as well as XML), is getting pegged by the compiler and it is giving me an error that translates to (I'm on a Japanese-language version of Flash) "A class instance variable can only be instantiated at compile time."
I'm confused because as far as I know, if the code is compiling, it IS compile time. And this code should have been OK, since the error came up when I compiled it.
Odd.
Thanks!
SS
Update: Here is the (stripped, to avoid showing any confidential info) External class for comparison:
import mx.xpath.XPathAPI;
class External {
private var _rawXML = new XML();
private var _xmlLocation:String = '';
public function External() {
trace('Loading external information...');
_rawXML.ignoreWhite = true;
_rawXML.onLoad = function(success:Boolean) {
if (success) {
trace('XML data was successfully loaded.')
initXML();
} else {
trace('There was an error loading the XML. Please check the XML file.');
}
}
preLoadXML();
}
private function loadLocalXML():Void {
getXML();
}
private function loadNetworkXML():Void {
getXML();
}
function getXML():Void {
if ( _rawXML.load( _xmlLocation ) ) {
trace('XML loading...');
} else {
trace('I\'m having difficulty finding the XML. You might want to check your data source.')
}
}
function initXML():Void {
_root._ary = XPathAPI.selectNodeList(this.rawXML, eventPath);
_root._total = _ary.length;
_root.siteLink = getXRedirect();
}
}
Upvotes: 1
Views: 485
Reputation: 1181
The english error is: "A class's instance variables may only be initialized to compile-time constant expressions."
From my experience it goes something like this:
Think about class definitions as the "blueprint" on how each instance of that class should be created.
In your example, you define that each instance of "Main" should contain a definition of the class External and then you create an instance of that class and binds it to the variable externals [Main.externals]. The issue here is that that code [the definition of Main] is only run once [static, during startup when it loads all the definitions into memory] so each subsequent instance of Main would always be linked to the same instance of "External". Since you didn't mention in the code that you wanted this to be the case, you are provided with this error.
So, what you probably would like to do, is to inside the constructor of Main, create the instance of External --
public var externals:External;
public function Main() {
_root._quality = 'BEST';
externals = new External();
trace('----------------------------------------------');
}
That way, each instance of Main would receive its own instance of external. That at least solves your issue and would be more "correct" when it comes to how classes/instances work in coding languages.
If you, on the other hand, want ALL instances of Main to be linked to the same instance of External, you should just write
public static var externals:External = new External();
public function Main() {
_root._quality = 'BEST';
trace("external=" + externals);
trace('----------------------------------------------');
}
Hope this makes sense, I realized that it was a bit hard to explain what I mean... :o
Upvotes: 1