2012-10-17 87 views
2

第一次海報在這裏。AS3公共變量不會存儲值

所以我建立一個「多RSS閱讀器」和我分成2班,「TechFeed.as」和「Feed.as」這一點;技術提要通過將URL傳遞給新的Feed對象來實例化多個提要。

我現在的問題是,在Feed.as中,我有一對公共變量,它們在TechFeed.as中用於顯示有關Feed的基本細節,但是,這些變量拒絕保留我給它們的任何值Feed.as的功能,並打開「空」。

編輯:也可能要注意; TechFeed.as是舞臺使用的主要AS文件。

TechFeed.as

package { 

    import flash.display.MovieClip; 

    public class TechFeed extends MovieClip { 

     private var feed_one:Feed = new Feed("http://feeds.feedburner.com/crunchgear"); 
     private var feed_two:Feed = new Feed("http://feeds.mashable.com/Mashable"); 
     private var feed_three:Feed = new Feed("http://www.engadget.com/rss.xml"); 

     public function TechFeed() { 
      trace(feed_one.feed_title); 

      //feed_name.text = feed_one.getFeedTitle(); 
     } 

    } 

} 

Feed.as

package { 

    import flash.net.URLLoader; 
    import flash.events.Event; 
    import flash.net.URLRequest; 

    public class Feed { 

     //Public variables to display feed info 
     public var feed_title:XMLList; 
     public var feed_desc:String; 
     public var feed_link:String; 

     //Setting up variables which help load the feeds 

     private var feedXML:XML; 
     //Create a loader to load an external URL 
     private var loader:URLLoader = new URLLoader(); 


     public function Feed(inURL:String = "") { 
      //Load the xml document 
      loader.load(new URLRequest(inURL)); 
      loader.addEventListener(Event.COMPLETE,onLoaded); 
     } 

     //When the loader had loaded the xml document, pass that into a variable for use. 
     private function onLoaded(e:Event):void { 
      feedXML = new XML(e.target.data); 

      // break down the xml document elements into singular XML array lists 

      //Feed details 
      this.feed_title = feedXML.channel.title; 
      this.feed_link = feedXML.channel.link; 
      this.feed_desc = feedXML.channel.description; 

      trace(this.feed_title); 

     } 

    } 

} 

任何幫助,將不勝感激:)

感謝, 傑夫

+0

加載是異步操作,所以你必須要等到XML被加載,比讀「FEED_TITLE」價值 – turbosqel

+0

@turbosqel我怎麼知道什麼時候完成?我不知道應該在dispatchEvent中放入什麼答案... – Geoff

回答

1

你的問題是時間,你正在試圖將其從服務器返回之前,從進料寫入數據。
在這個例子中,我分配了一個函數在數據加載時回調。

包{

import flash.display.MovieClip; 

    public class TechFeed extends MovieClip { 

     private var feed_one:Feed; 
     private var feed_two:Feed; 
     private var feed_three:Feed; 

     public function TechFeed() { 
      feed_one= new Feed("http://feeds.feedburner.com/crunchgear",assignResults); 
      feed_two= new Feed("http://feeds.mashable.com/Mashable",assignResults); 
      feed_three= new Feed("http://www.engadget.com/rss.xml",assignResults); 


     } 

     public function assignResults(value:value):void{ 
      feed_name.text = value; 
     } 

    } 

} 






package { 

    import flash.net.URLLoader; 
    import flash.events.Event; 
    import flash.net.URLRequest; 

    public class Feed { 

     //Public variables to display feed info 
     public var feed_title:XMLList; 
     public var feed_desc:String; 
     public var feed_link:String; 

     //Setting up variables which help load the feeds 

     private var feedXML:XML; 
     //Create a loader to load an external URL 
     private var loader:URLLoader = new URLLoader(); 

private var _callBackFunc:Function; 
     public function Feed(inURL:String = "", callBackFunc:Function) { 


      this._callBackFunc = callBackFunc; 


      //Load the xml document 
      loader.load(new URLRequest(inURL)); 
      loader.addEventListener(Event.COMPLETE,onLoaded); 
     } 

     //When the loader had loaded the xml document, pass that into a variable for use. 
     private function onLoaded(e:Event):void { 
      feedXML = new XML(e.target.data); 

      // break down the xml document elements into singular XML array lists 

      //Feed details 
      this.feed_title = feedXML.channel.title; 
      this.feed_link = feedXML.channel.link; 
      this.feed_desc = feedXML.channel.description; 

      this._callBackFunc(this.feed_title); 

     } 

    } 

} 
+0

謝謝!你給了我一個很好的起點來建立:) – Geoff

1

如果問題在於TechFeed()中存在feed_one.feed_title null,那是因爲您並未等待Feed完成加載。 當你完成加載和處理數據時,你應該做的是從Feed中發送一個事件,在TechFeed中捕獲它,然後使用公共變量(這也意味着你的Feed類必須繼承EventDispatcher的子類) :

飼料類:

private function onLoaded(e:Event):void { 
      feedXML = new XML(e.target.data); 

      // break down the xml document elements into singular XML array lists 

      //Feed details 
      this.feed_title = feedXML.channel.title; 
      this.feed_link = feedXML.channel.link; 
      this.feed_desc = feedXML.channel.description; 

      trace(this.feed_title); 
      dispatchEvent(new Event(Event.COMPLETE));////Dispatch Event 

     } 

在TechFeed類:

public function TechFeed() { 
      trace(feed_one.feed_title);//This will trace "null" 
      feed_one.addEventListener(Event.COMPLETE, dataLoaded);//add event listener 
     } 
private function dataLoaded(e:Event):void{ 
      var feed = Feed(e.currentTarget); 
      feed.removeEventListener(Event.COMPLETE, dataLoaded);//remove event listener to prevent memory leaks 
      trace(feed.feed_title);// This will trace the correct title 
     } 
+0

感謝您的迴應!但是我不確定會發生什麼事情到dispatchEvent函數?它對事件有什麼影響(如果有的話)。 – Geoff

+0

@Geoff我不知道我理解你的問題。謹慎改寫? –

+0

無後顧之憂,全部在答案中解決,謝謝:) – Geoff