Actionscript 3 AS3公共变量赢得';t存储值
第一次在这里张贴海报 因此,我正在构建一个“多RSS提要阅读器”,并将其分为两类,一类是“TechFeed.as”,另一类是“feed.as”;技术提要通过将URL传递给新的提要对象来实例化多个提要 我目前的问题是,在Feed.as中,我有两个公共变量,它们在TechFeed.as中使用,用于显示有关Feed的基本细节,但是,这些变量拒绝保留我在Feed.as的函数中给它们的任何值,并显示为“null” 编辑:也可能是需要注意的东西;TechFeed.as是stage使用的主要as文件 TechFeed.asActionscript 3 AS3公共变量赢得';t存储值,actionscript-3,oop,Actionscript 3,Oop,第一次在这里张贴海报 因此,我正在构建一个“多RSS提要阅读器”,并将其分为两类,一类是“TechFeed.as”,另一类是“feed.as”;技术提要通过将URL传递给新的提要对象来实例化多个提要 我目前的问题是,在Feed.as中,我有两个公共变量,它们在TechFeed.as中使用,用于显示有关Feed的基本细节,但是,这些变量拒绝保留我在Feed.as的函数中给它们的任何值,并显示为“null” 编辑:也可能是需要注意的东西;TechFeed.as是stage使用的主要as文件 Tech
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();
}
}
}
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);
}
}
}
Feed.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();
}
}
}
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);
}
}
}
任何帮助都将不胜感激:)
谢谢,
Geoff如果问题是TechFeed()构造函数中的
feed\u one.feed\u title
出现空值,那是因为您没有等待feed完成加载。
您需要做的是在加载和处理完数据后从提要中调度一个事件,在TechFeed中捕获它,然后根据需要使用公共变量(这也意味着您的提要类必须为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
}
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
}
在TechFeed类中:
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
}
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
}
您的问题是,在从服务器返回数据之前,尝试从提要写入数据的时间
在本例中,我分配了一个函数,以便在加载数据时调用 包装{
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);
}
}
}
感谢您的回复!但是我不确定dispatchEvent函数将包含什么?它与事件有什么关系(如果有的话)@Geoff我不确定我是否理解您的问题。是否要重新措辞?加载是异步操作,因此您必须等待XML加载,而不是读取“提要标题”value@turbosqel我怎么知道它什么时候结束?我不知道在答案中应该在dispatchEvent中添加什么…谢谢!您为我提供了一个很好的起点:)