AS3公共变量不会存储值

时间:2012-10-17 05:14:46

标签: actionscript-3 oop

这里的第一次海报。

所以我正在建立一个“多RSS提要阅读器”,我把它分为两类,一个是“TechFeed.as”和一个“Feed.as”;技术提要通过将URL传递给新的Feed对象来实例化多个提要。

目前我的问题是在Feed.as中有一些公共变量在TechFeed.as中用于显示有关Feed的基本详细信息,但是,这些变量拒绝保留我在Feed中提供的任何值.as的功能,并且正在变为“null”。

编辑:也许有些事情需要注意; 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);

        }

    }

}

任何帮助将不胜感激:)

谢谢, 杰夫

2 个答案:

答案 0 :(得分:1)

如果问题是TechFeed()ctor中feed_one.feed_title出现空值,那是因为您没有等待Feed完成加载。 您需要做的是在Feed完成加载和处理数据后从Feed中调度事件,在TechFeed中捕获它,然后根据需要使用公共变量(这也意味着您的Feed类必须继承EventDispatcher):

在Feed类中:

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
        }

答案 1 :(得分: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);

        }

    }

}