2011-05-04 172 views
1

我在使用flash创建的类中存在范围问题。我正在创建一个应用程序,它利用XML来创建一系列信息,我将操纵并重新保存到XML中。AS3范围问题

我有一个文档类调用调用另一个类,我正在使用加载XML,将其转换为数组并将数组返回到使用方法的文档构造函数。我已经成功解析了XML并创建了一组数据,但似乎无法通过loadXMLData类中的processXML函数向数组添加数据。

我从代码中剔除了所有无关紧要的东西。这是我想要做的基本表示。

我的文档类

package { 

    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.utils.*; 


    public class dashBoard extends MovieClip { 

     public var newData 

     //initialize the dashBoard 
     public function dashBoard(){ 

      //construct the Dashboard Object 
      trace("Dashboard Initialized"); 
      trace("|--------------XML Data--------------|"); 
      newData = new loadXMLData(); 
      trace(newData.getSections()); 


     } 

    } 

} 

我loadXMLData类

package { 

     import flash.display.MovieClip; 
     import flash.events.Event; 
     import flash.net.URLLoader; 
     import flash.net.URLRequest; 
     import flash.utils.*; 

     public class loadXMLData extends MovieClip { 

       //initialize an array that will be used in the document class 
       public var sectionList:Array = new Array(); 

       public function loadXMLData() { 

        //load the xml file containing the data 
        var myLoader = new URLLoader(); 
        myLoader.load(new URLRequest("dashboard.xml")); 
        myLoader.addEventListener(Event.COMPLETE, processXML); 

       function processXML(e:Event):void { 

        //process the xml file after it loads 
          //and create an object 
        var newXML:XML = new XML(e.target.data); 

          //then I use the XML to populate my 
          //array i declared at the top 
          //this is a simple test 
          sectionList[0] = "test"; 

       } 

      } 

       //and this is the method i use in the document class 
       //to get the sectionList array 
      public function getSections():Array{ 
       return sectionList; 
       } 

     } 

    } 

似乎很简单,但我似乎无法编辑阵列。文档类总是返回一个空白数组。有任何想法吗?

回答

1

你试图访问一个当XML加载完成后,像这样没有在你的loadXMLData.processXML()功能exist.Dispatch事件的信息:

function processXML(e:Event):void 
{ 

    //process the xml file after it loads 
    //and create an object 
    var newXML:XML = new XML(e.target.data); 

    dispatchEvent(new Event("xml_loaded")); 

} 

然后听它,你做任何事情之前,像这样:

package 
{ 
    import flash.display.MovieClip; 
    import flash.events.Event; 
    import flash.net.URLLoader; 
    import flash.net.URLRequest; 
    import flash.utils.*; 


    public class dashBoard extends MovieClip 
    { 

     public var newData:loadXMLData; 

     //initialize the dashBoard 
     public function dashBoard(){ 

      //construct the Dashboard Object 
      trace("Dashboard Initialized"); 
      newData = new loadXMLData(); 

      newData.addEventListener("xml_loaded", _xmlLoaded); 
     } 

     private function _xmlLoaded(e:Event):void 
     { 
      trace("|--------------XML Data--------------|"); 
      trace(newData.getSections()); 
      newData.removeEventListener("xml_loaded", _xmlLoaded); 
     } 
    } 
} 
+0

你是最棒的!感谢你的帮助。我是面向对象编程的新手,我刚开始看到它有多强大。 – Laurence 2011-05-04 05:24:11

0

这不是一个范围问题,您构建loadXMLData,然后立即尝试获取sectionList,而无需等待它加载。构造函数调用和getSection调用是同步的,但实际的XML文件加载是异步的。

理想情况下,你的loadXMLData对象将从的processXML处理函数,你的文档类侦听派遣一个完整的事件(就像你loadXMLData侦听Loader来完成)

1

有一个非常现实的可能性行: trace(newData.getSections()); 发生在函数processXML之前,这是什么填充数组。这是因为processXML正在响应Event.COMPLETE,可能需要几秒钟才能完成。

在尝试输出事件之前,您的文档还应该“侦听”事件(您的加载器类应该派发)。