如何使事件监听器函数返回一个值?如何跟踪侦听器功能的响应?

时间:2013-10-15 10:05:04

标签: actionscript-3

我试着编写一个类,它将返回给定字符串的json对象。我知道下面代码的语法是完全错误的,但只是尝试这样做。

package com 
{
    import flash.display.MovieClip;
    import com.adobe.*;
    import com.adobe.serialization.json.JSON;
    import flash.net.dns.AAAARecord;
    import flash.net.URLRequest;
    import flash.events.Event;

    public class getJson extends MovieClip
    {

        public function getJson(urlString:String):Object
        {
            // constructor code
            var returnObject:Object = new Object();
            var urlLoader:URLLoader = new URLLoader();
            returnObject=urlLoader.addEventListener(Event.COMPLETE,loadingComplete);
            urlLoader.load(new URLRequest(urlString));
            return returnObject;


        }

        private function loadingcomplete(e:Event):Object
        {
            var jsonObject:Object = new Object();
            jsonObject=JSON.decode(urlString);
            return jsonObject;
        }

    }

}

1 个答案:

答案 0 :(得分:-1)

试试这个:

  public var urlLoader:URLLoader;
  public  String url;
   public function getJson(urlString:String):Object
    {
        url=urlString;            
        var returnObject:Object = new Object();
        urlLoader = new URLLoader();
        urlLoader.addEventListener(ProgressEvent.PROGRESS, onProgress);
        urlLoader.load(new URLRequest(urlString));
        returnObject=urlLoader.addEventListener(Event.COMPLETE,onComplete);            
        return returnObject;

    }
   function onProgress(e:ProgressEvent):void
  {
    trace("Progressing");
  }
   function onComplete(e:Event):void
  {
    trace("Fully loaded");      
    urlLoader.removeEventListener(ProgressEvent.PROGRESS, onProgress);
    urlLoader.removeEventListener(Event.COMPLETE, onComplete);
    var jsonObject:Object = new Object();
    jsonObject=JSON.decode(url);
    return jsonObject
  }