Actionscript 3 - 如何在删除该事件后保留事件的值

时间:2011-12-04 19:35:19

标签: flash actionscript-3 flash-builder flash-cs5 flash-cs4

我在我的事件处理函数(函数在event.COMPLETE之后执行)期间为我的xml对象(xml = new XML(e.currentTarget.data);)设置了一个值,如果我在我的事件中跟踪对象函数处理程序,它显示我的xml数据。

但是如果我尝试在事件处理函数之外跟踪它,它就不会显示我的xml内容。有没有办法让我的xml对象内容值显示在另一个函数中而不是在事件处理函数中?

private var xml:XML; 

public function XMLLoader(xmlURL:String) 
{ 
    var xmlURLRequest:URLRequest = new URLRequest(xmlURL); 
    var xmlURLLoader:URLLoader  = new URLLoader(xmlURLRequest); 
    xmlURLLoader.addEventListener(Event.COMPLETE, xmlData); 
}

private function xmlData(e:Event):void 
{
    e.currentTarget.removeEventListener(Event.COMPLETE, xmlData); 
    xml = new XML(e.currentTarget.data); 
    dispatchEvent(new Event(Event.COMPLETE)); 
    trace(xml); 
} 

public function getXMLData():void 
{ 
     //I've find out that this shows null because this function is faster
     //what do i do? put an event.complete in every following function?
    trace(xml);
}

感谢。

4 个答案:

答案 0 :(得分:1)

我认为您在getXMLData()之后立即致电XMLLoader() xml对象仍然为空。尝试在 getXMLData()函数中调用xmlData() ,您应该会看到差异。

答案 1 :(得分:0)

你可能在URLLoader完成之前调用了getXMLData(),但是,一般来说,你正在执行的代码是不好的做法。 Actionscript没有任何阻塞,因此当您使用URLRequest实例化新的URLLoader时,它会立即开始加载操作。例如,如果您的文件被缓存,则在开始加载操作后,您的Event.COMPLETE侦听器可能永远不会触发。

我已经在创建和加载数据的理想过程中添加了以下内容:

var data:XML;
var urlLoader:URLLoader = new URLLoader();

urlLoader.addEventListener(Event.COMPLETE, urlLoader_completeHandler);
urlLoader.load(new URLRequest("path"));

private function urlLoader_completeHandler(event:Event):void
{
    data = new XML(urlLoader.data);
}

如果你想将它应用到你自己的XMLLoader类中,我在AS3中包含了“正确”的实现......但是非常详细。

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

class XMLLoader extends URLLoader
{
    public function XMLLoader()
    {
        super();
        addEventListener(Event.COMPLETE, completeHandler);
    }

    private function completeHandler(event:Event):void
    {
        dispatchEvent(new XMLLoaderEvent(XMLLoaderEvent.COMPLETE, new XML(data)));
    }
}

class XMLLoaderEvent extends Event
{
    public static const COMPLETE:String = "xmlLoaderComplete";

    public var data:XML;

    public function XMLLoaderEvent(type:String, data:XML = null, bubbles:Boolean = false, cancelable:Boolean = false)
    {
        super(type, bubbles, cancelable);

        this.data = data;
    }

    override public function clone():Event
    {
        return new XMLLoaderEvent(type, data, bubbles, cancelable);
    }
}

使用XMLLoader类:

var xmlLoader:XMLLoader = new XMLLoader();
xmlLoader.addEventListener(XMLLoaderEvent.COMPLETE, function(event:XMLLoaderEvent):void
{
    trace(event.data);
});

xmlLoader.load(new URLRequest("data.xml"));

祝你好运!

答案 2 :(得分:0)

更改trace方法以指示xml是否已加载...
你正在陷入竞争状态

private var _xmlLoaded:Boolean=false;// add this to see if the xml is loaded
private function xmlData(e:Event):void 
{
    e.currentTarget.removeEventListener(Event.COMPLETE, xmlData); 
    xml = new XML(e.currentTarget.data); 
    _xmlLoaded=true;
    dispatchEvent(new Event(Event.COMPLETE)); 
    trace(xml); 
} 
public function getXMLData():void 
{ 
    if (_xmlLoaded)
        trace(xml);
    else
        trace("not yet loaded");
}

答案 3 :(得分:0)

//注意:如果您希望直接访问此“类”的功能而不使用任何事件上的类别,请使用此功能,原因是它已经在每个功能上进行了事件监听(这只会导致一个功能getXMLData() )

private var xml:XML;

public function XMLLoader(xmlURL:String) {

     var xmlURLRequest:URLRequest = new URLRequest(xmlURL); 
     var xmlURLLoader:URLLoader  = new URLLoader(xmlURLRequest); 
     xmlURLLoader.addEventListener(Event.COMPLETE, xmlData); 

}

私有函数xmlData(e:Event):void {

     e.currentTarget.removeEventListener(Event.COMPLETE, xmlData); 
     xml = new XML(e.currentTarget.data); 
     dispatchEvent(new Event(Event.COMPLETE)); 
     trace("1");//this used to come second of getXMLData() but it's solved now
     trace(xml); 

}

public function getXMLData():void {

     //This function was coming first so if you don't want to use an event listener outside    
     //this class to wait for event.complete you can use it here to wait for it and access
     //the function directly without being afraid of the object being null:
     addEventListener(Event.COMPLETE, go)
     function go(e:Event){
          trace("2"); //now it ONLY comes AFTER the event.complete, no need for external                   listeners over this class. declare the class object and use getXMLData() directly cause it always comes second the event handler xmlData() :)
          trace(xml);
     }
}