Config.as 加载一个 XML 文件并将 XML 文件的内容存储在静态变量“xml”中。我试图在 Main.as 类中访问这个变量,但它返回一个空字符串。
我认为问题在于加载过程。我试图在 XML 文件实际加载之前访问该变量。有任何想法吗?
public static var xml:XML; // returns null
public static var xml:XML = new XML(); //returns ""
public class Config {
public static var xml:XML = new XML();
//public static var xml:XML;
public function Config() {
loadXML("config.xml");
}
private function loadXML(url:String):void{
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onCompleteXML);
loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
loader.load(new URLRequest(url));
}
private function onCompleteXML(e:Event):void{
xml = new XML(e.target.data);
trace("loaded");
}
private function onIOError(e:IOErrorEvent):void{
trace("Load operation failed!" + e.text);
}
public static function getXML():XML{
return Config.xml;
}
public class Main extends MovieClip {
public function Main () {
new Config();
trace(Config.xml);
trace(Config.getXML());
}
}
编辑:
public class Main extends MovieClip
{
public function Main ()
{
new Config();
}
public static function xmlHasLoaded():void
{
//now do something
trace(Config.xml);
newFunction();
}
public function newFunction():void{ //don't work
trace("new function"); //public static function newFunction():void{} works
}
}