动作3-如何在移除事件后保留事件中的值 [英] Actionscript 3 - How to keep the value from an event after removing that event

查看:85
本文介绍了动作3-如何在移除事件后保留事件中的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在事件处理程序函数(该函数在event.COMPLETE之后执行)期间以及是否在事件内部跟踪该对象时,为我的xml对象(xml = new XML(e.currentTarget.data);)设置了一个值函数处理程序,它显示了我的xml数据.

I set a value to my xml object (xml = new XML(e.currentTarget.data);) during my event handler function (the function is executed after the event.COMPLETE) and if I trace the object inside my event function handler it shows my xml data.

但是,如果我尝试在事件处理程序函数之外跟踪它,则不会显示我的xml内容.没有办法让我的xml对象内容值显示在其他函数中,而不显示在事件处理程序函数中吗?

but if I try to trace it outside the event handler function it doesn't show my xml content. Isn't there a way to get my xml object content value to show in an other function but not in the event handler function?

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);
}

谢谢.

推荐答案

//注意:如果您要直接访问此班级"的功能而无需在班级上使用任何活动的语音提示,请使用此功能,因为它已经存在每个函数的事件听众(这是因为只有一个函数getXMLData())

//NOTE: USE THIS IF YOU WANT TO ACCESS DIRECTLY TO THE FUNCTIONS OF THIS "CLASS" WITHOUT USING ANY EVENT LISTENER ON THE CLASS CAUSE IT ALREADY HAS ITS EVENT LISTENER ON EACH FUNCTION (THIS CAUSE ONE FUNCTION ONLY getXMLData())

private var xml:XML;

private var xml:XML;

公共函数XMLLoader(xmlURL:String){

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{

private function 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); 

}

公共函数getXMLData():void{

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);
     }
}

这篇关于动作3-如何在移除事件后保留事件中的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆