Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/actionscript-3/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Actionscript 3 ActionScript3:在addEventListener读取的一个类文件中,如何将数据传递给另一个类_Actionscript 3_Flash - Fatal编程技术网

Actionscript 3 ActionScript3:在addEventListener读取的一个类文件中,如何将数据传递给另一个类

Actionscript 3 ActionScript3:在addEventListener读取的一个类文件中,如何将数据传递给另一个类,actionscript-3,flash,Actionscript 3,Flash,有许多问题与我的问题相似,但没有一个能解决我的问题 我有这门课- package com.test { import flash.events.*; import flash.net.*; import com.test.LogUtils; import mx.logging.ILogger; public class LoadExtUrl extends EventDispatcher { private va

有许多问题与我的问题相似,但没有一个能解决我的问题

我有这门课-

package com.test
{
    import flash.events.*;
    import flash.net.*;
    
    import com.test.LogUtils;
    import mx.logging.ILogger;
    
    public class LoadExtUrl extends EventDispatcher
    {
        private var baseUrl:String;
        private var log:ILogger = LogUtils.getLogger(LoadExtUrl);
        
        public function LoadExtUrl()
        {
            log.debug ("100 In LoadExtUrl()");
            super(null);
        }
        
        public function loadBaseUrl():String
        {
            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader.addEventListener(Event.COMPLETE, urlLoader_completeHandler);
            
            function urlLoader_completeHandler(event:Event):void
            {
                var loader:URLLoader = URLLoader(event.target);
                this.baseUrl = loader.data.baseurl;
                dispatchEvent(new Event("GOTRESULTS"));
                log.debug ("200 In LoadExtUrl, baseUrl="+this.baseUrl);
            }
            
            loader.load(new URLRequest("sri-config-files/url.properties"));
            
            log.debug ("300 In LoadExtUrl, baseUrl="+this.baseUrl);
            return this.baseUrl;
        }
    }
}
现在我想在许多其他类中读取baseUrl的值

在另一个类中,我有以下代码-

public class UrlHelper
{
    public static var myLoadExtUrl:LoadExtUrl = new LoadExtUrl();
    public static var baseUrl:String;
    
    public function UrlHelper()
    {}
    
    public static function getBaseUrl():void
    {
        myLoadExtUrl.addEventListener("GOTRESULTS", xmlLoadCompleted);
        log.debug("400 In UrlHelper, baseUrl ="+baseUrl);
    }
    
    private static function xmlLoadCompleted(e:Event):void 
    {
        baseUrl=myLoadExtUrl.loadBaseUrl();
        log.debug("500 In UrlHelper, baseUrl ="+baseUrl);
    }
}
测井序列-

100 In LoadExtUrl()
300 In LoadExtUrl, baseUrl=null
200 In LoadExtUrl, baseUrl=http://abcxyz.com:8080/

400 In UrlHelper, baseUrl =null --> here only I need the value

我怎样才能解决这个问题?

我想我需要写下一些解释

什么是异步操作?这是一项

需要一些初始未知或不确定的时间才能完成 不阻止代码执行,简单地说,当您开始加载代码时,它不会停止等待操作完成,它会立即开始执行代码的其余部分,而不考虑操作状态 因此,您正在构建的事物中的实际事件流是:

UH类告诉LEU类开始加载 ... 一段时间过去了。。。 LEU类检测加载过程的结束。 加载的数据可用。 LEU发送自定义事件。 UH检测到事件并最终获得数据。 因此,LoadExtUrl类:

package
{
    import flash.events.Event;
    import flash.events.EventDispatcher;
    
    import flash.net.URLLoader;
    import flash.net.URLRequest;
    import flash.net.URLLoaderDataFormat;
    
    public class LoadExtUrl extends EventDispatcher
    {
        private var baseUrl:String;
        private var loader:URLLoader;
        
        // Interface method.
        public function loadBaseUrl():String
        {
            var aRequest:URLRequest;
            
            // Form the HTTP request.
            aRequest = new URLRequest;
            aRequest.url = "sri-config-files/url.properties";
            
            // Initiate the loading process.
            loader = new URLLoader;
            loader.dataFormat = URLLoaderDataFormat.VARIABLES;
            loader.addEventListener(Event.COMPLETE, onLoad);
            loader.load(aRequest);
            
            // Data are NOT available yet at this point.
        }
        
        // Data loading COMPLETE handler.
        private function onLoad(e:Event):void
        {
            // Data are AVAILABLE at this point.
            
            // Extract the data.
            baseUrl = loader.data.baseurl;
            
            // Clean up.
            loader.removeEventListener(Event.COMPLETE, onLoad);
            loader = null;
            
            // Tell anyone willing to listen about the data availability.
            var anEvent:Event;
            
            // Feel free to use predefined constants instead of custom
            // event names. It will protect you against typo errors.
            anEvent = new Event(Event.COMPLETE);
            dispatchEvent(anEvent);
        }
    }
}
如何使用它:

public class UrlHelper
{
    static public var baseUrl:String;
    
    static private var loadExt:LoadExtUrl;
    
    // Interface method.
    static public function getBaseUrl():void
    {
        // Data are NOT available yet at this point.

        loadExt = new LoadExtUrl;

        // Data are NOT available yet at this point.
        
        // Subscribe to the very same event name
        // that class is going to dispatch.
        loadExt.addEventListener(Event.COMPLETE, onAnswer);

        // Data are NOT available yet at this point EITHER.
        // Loading is an asynchronous operation. We started
        // the loading but we have to wait until the data are available.
    }
    
    // This handler will be invoked when data are available.
    static private function onAnswer(e:Event):void 
    {
        // Data are AVAILABLE at this point.
        
        // Extract the data.
        baseUrl = loadExt.baseUrl;
        
        // Clean up.
        loadExt.removeEventListener(Event.COMPLETE, onAnswer);
        loadExt = null;
        
        // You are free to use the obtained data at this point.
        // ...
    }
}

因为sendUrl。。。方法不返回任何内容。因此,baseUrl=myLoadExtUrl.sendUrl不能得到任何东西;你好@Organi:谢谢。我更正了代码。实际上,我尝试了这么多选项,并在这里粘贴了错误的代码。sendUrl没有返回任何内容,它是空的。但是在sendUrl中,也在调用addEventListener之后,该值为null。因为addEventListener不是阻塞调用。因此,我尝试在sendUrl中获取值并返回它,但即使这样它也不起作用-公共函数sendUrl:String{…….loader.addEventListenerEvent.COMPLETE,handleComplete;loader.loadrequest;tracebaseUrl;->NULL return baseUrl;}根据定义,加载是异步的,因此,只有在收到加载程序发送的Event.COMPLETE时,您才会收到有效的baseUrl。因此,您需要执行myLoadExtUrl.addEventListenerEvent.COMPLETE,GetHatUrl;函数getThatUrle:Event=null{this.baseUrl=myLoadExtUrl.baseUrl;}Hi@Vesper:yes,我已经做过了。请检查UrlHelper.xmloadCompleted函数。我把它标为烈性的。在这里,我将baseUrl=myLoadExtUrl.baseUrl;我在这个函数中获取值,但在UrlHelper.getBaseUrl中没有。如何解决它?@NirmalyaSinha好的,现在你甚至没有尝试将任何内容放入UrlHelper.baseUrl中,所以它显然是空的。另外,正如Vesper所指出的,如果你从加载的数据中获取数据,那么很明显,在你开始加载过程的那一刻,它是不可用的。谢谢@Organi,这消除了我的疑问。在我的代码中,我也得到了同样的行为,并怀疑这是否正确。看起来代码的剩余部分将保留在onAnswer函数中。谢谢您的回答。@NirmalyaSinha确切地说,代码的其余部分应该放在onAnswer中,因为它是您需要的数据可用的地方。