【问题标题】:Make a parent function return in ActionScript3? (or make addEventListener return)在 ActionScript3 中返回父函数? (或使 addEventListener 返回)
【发布时间】:2010-11-21 16:50:09
【问题描述】:

基本上我有一个应该加载大量图像的代码,所以我需要一个函数来加载它们。问题是 addEventListener 需要一个函数,当事件引发时它将调用该函数。

我需要找到一种方法,让 loadImage 函数在事件引发后返回ONLY,或者让 addEventListener 中引发的函数返回它的父函数。

    function loadImage(imagePath:String)//:BitmapData
    {
            var internalLoader:Loader = new Loader();

            var internalImageRequest = new URLRequest(imagePath); 

            var loadingComplete:Boolean = false;

            internalLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, 
                                                              function (event:Event):void
                                                              {
                                                                loadingComplete = true;
                                                              }
                                                              );
            internalLoader.load(internalImageRequest);



            //I need to wait 'till the event have been raised and then RETURN , NOT BEFORE THAT
            //var tempImg:Bitmap = Bitmap(internalLoader.content);
            //return tempImg.bitmapData;
    }

提前致谢

【问题讨论】:

    标签: flash actionscript-3 return-value return


    【解决方案1】:

    简答:放弃吧,那样不会发生的。

    长答案:这里的事情是你永远不想让 Flash Player 挂起。原因很多,但归结为您总是在 UI 线程中操作。像您这样的网络请求意味着您必须等待图像加载,这会在等待时挂起 UI,从而使用户感到烦恼。如果图像很小并且网络效率不是问题,那么这对您来说不是问题。但是,如果图像需要一段时间才能加载,问题就会出现。此外,如果我没记错的话,Flash Player 会限制每帧的代码执行时间,为 15 秒。之后,用户会收到一个弹出窗口,询问是否应该中止脚本。

    我建议您改为定义一个事件处理程序,该处理程序对您从加载程序获得的 BitmapData 对象执行您需要执行的任何操作。

    function loadImage(imagePath:String):void
    {
        var internalLoader:Loader = new Loader();
        var internalImageRequest = new URLRequest(imagePath); 
    
        var loadingComplete:Boolean = false;
    
        internalLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, onLoad);
        internalLoader.load(internalImageRequest);
    }
    
    function onLoad(event:Event):void
    {
        // Do whatever you need to do with the loader here
        var contentLoaderInfo:LoaderInfo = LoaderInfo(event.target);
    }
    

    【讨论】:

      【解决方案2】:

      很遗憾,这是不可能的。您需要父函数注册的事件。然后当加载器完成加载时,它会引发事件。您可以在 eventargs 中传递您的位图数据。

      function (event:Event):void 
      {
          loadingComplete = true;
          dispatchEvent([Your Event]);
      }
      

      您的父母需要倾听 [Your Event] 并做任何需要的事情。

      【讨论】:

        【解决方案3】:

        您也可以尝试继续传递样式:

        function loadImage(path:String, result:Function):void {
            var l:Loader = new Loader();
            l.load(new URLRequest(path));
            l.contentLoaderInfo.addEventListener(Event.COMPLETE, function(event:Event):void {
                result(Bitmap(l.content).bitmapData);
            });
            // If you want to handle IO errors, otherwise this effectively never "returns".
            l.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR, function(event:Event):void {
                result(null);
            });
        }
        
        function process(path:String):void {
            loadImage(path, function(data:BitmapData):void {
                ...
            }
        }
        

        当您必须链接多个函数时,这会变得非常痛苦,因此您可能会破坏标准格式:

        function process(path:String):void {
            loadImage(path, function(
            data:BitmapData):void { // Async "result declaration"
        
            longProcessing(data, function(
            result:ProcessingResult):void {
        
            // Can use data and result here...
        }}} // Have to close continuations as well as process
        

        【讨论】:

          猜你喜欢
          • 2021-10-02
          • 2014-02-12
          • 2010-10-01
          • 2018-11-13
          • 2015-02-13
          • 2020-07-13
          • 2014-01-31
          • 1970-01-01
          • 2017-07-16
          相关资源
          最近更新 更多