2011-05-04 170 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,可能需要幾秒鐘才能完成。

在嘗試輸出事件之前,您的文檔還應該「偵聽」事件(您的加載器類應該派發)。