AS3从事件侦听器函数返回变量

时间:2011-09-28 18:19:42

标签: flash actionscript-3 flash-cs5

我正在尝试将XML从事件完整返回到main函数,但无法弄清楚如何执行此操作。

这是我正在调用的函数:

主档案:

public var mySendAndLoad:SendAndLoad = new SendAndLoad(); 

mySendAndLoad.sendData(url,variables)

mySendAndLoad类:

package 
{

    import flash.events.*;
    import flash.net.*;
    import flash.xml.XMLDocument;

    public class SendAndLoad
    {
        public var xml:XML = new XML();
        public var xmlList:XMLList;

        public function SendAndLoad()
        {
        }
        public function sendData(url:String, _vars:URLVariables):String
        {
            var request:URLRequest = new URLRequest(url);
            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            request.data = _vars;
            request.method = URLRequestMethod.POST;
            loader.addEventListener(Event.COMPLETE, handleComplete);
            loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            loader.load(request);
        }
        private function handleComplete(event:Event):String
        {
            var loader:URLLoader = URLLoader(event.target);
            xml = XML(loader.data);
            xmlList = xml.children();
            return(xmlList[1]);
        }
        private function onIOError(event:IOErrorEvent):void
        {
            trace("Error loading URL.");
        }
    }
}

我想要发生的是让xmlList回到主文件。但是因为它不在我调用的事件(sendData)中,所以它不会被带回来。

有什么想法吗?

5 个答案:

答案 0 :(得分:1)

package 
{

import flash.events.*;
import flash.net.*;
import flash.xml.XMLDocument;

//THIS CLASS EXTENDS EVENTDISPATCHER SO IT CAN DISPATCH EVENTS
public class SendAndLoad extends EventDispatcher
{
    public var xml:XML = new XML();
    public var xmlList:XMLList;

    public function SendAndLoad()
    {
        super(null);
    }
    public function sendData(url:String, _vars:URLVariables):void
    {
        var request:URLRequest = new URLRequest(url);
        var loader:URLLoader = new URLLoader();
        loader.dataFormat = URLLoaderDataFormat.TEXT;
        request.data = _vars;
        request.method = URLRequestMethod.POST;
        loader.addEventListener(Event.COMPLETE, handleComplete);
        loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
        loader.load(request);
    }
    private function handleComplete(event:Event):void
    {
        var loader:URLLoader = URLLoader(event.target);
        xml = XML(loader.data);
        xmlList = xml.children();


        //NEW CODE
        var evt:Event=new Event(Event.COMPLETE);
        dispatchEvent(evt);
        //return(xmlList[1]);
    }
    private function onIOError(event:IOErrorEvent):void
    {
        trace("Error loading URL.");
    }
}

}

并在主文件中

public var mySendAndLoad:SendAndLoad = new SendAndLoad(); 
mySendAndLoad.addEventListener(Event.COMPLETE, xmlLoadCompleted);
mySendAndLoad.sendData(url,variables)

private function xmlLoadCompleted(e:Event):void {
    var childToUse:XML=mySendAndLoad.xmlList[1];
    //whatever else you want to do 
}

答案 1 :(得分:0)

如果没有认真的修改,您有两种选择:

  1. 您的SaveAndLoad课程将驱逐EventDispatcher并发送包含数据的自定义事件。

  2. 更干净/非突兀的是进行sendData方法修改:

    public function sendData(url:String, _vars:URLVariables, resultHandler:Function=null):String
    {
        loader.addEventListener(Event.COMPLETE, resultHandler);
    }
    
  3. 您可以传递主文件中定义的函数,仍然可以在SaveAndLoad类中为您想要的内容添加内部侦听器,并且您将获得数据。

答案 2 :(得分:0)

只是派遣一个活动

package 
{

    import flash.events.Event;
    import flash.display.Sprite;
    import flash.events.*;
    import flash.net.*;
    import flash.xml.XMLDocument;

    public class SendAndLoad extend Sprite
    {
        public var xml:XML = new XML();
        public var xmlList:XMLList;

        public function SendAndLoad()
        {
        }
        public function sendData(url:String, _vars:URLVariables):String
        {
            var request:URLRequest = new URLRequest(url);
            var loader:URLLoader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            request.data = _vars;
            request.method = URLRequestMethod.POST;
            loader.addEventListener(Event.COMPLETE, handleComplete);
            loader.addEventListener(IOErrorEvent.IO_ERROR, onIOError);
            loader.load(request);
        }
        private function handleComplete(event:Event):String
        {
            var loader:URLLoader = URLLoader(event.target);
            xml = XML(loader.data);
            xmlList = xml.children();


           // return(xmlList[1]);
           this.dispatchEvent( new Event( 'ONDATA' ) );
        }
        private function onIOError(event:IOErrorEvent):void
        {
            trace("Error loading URL.");
        }
    }
}


// usage
var sender:SendAndLoad = new SendAndLoad( );
sender.addEventListener( 'ONDATA', myXMLLoaded);
sender.sendData( url, _vars )

function myXMLLoaded( e:Event ):void{
  trace(sender.xml);
}

答案 3 :(得分:0)

您必须使用dispatchEvent;

首先向它宣布    public class personal_info extends EventDispatcher {

并且您必须将侦听器定义到该类中    dispatchEvent(new Event(Event.COMPLETE));

答案 4 :(得分:0)

you must declare variable such as public
public var variable:String;

You must use dispatchEvent;

first declare to it
   public class personal_info extends EventDispatcher {
   ...

and you must define listener into that complete event
   variable = "variable";
   dispatchEvent(new Event(Event.COMPLETE));
   ...

And you must call just like calling normal event in another class
   var sample:SampleClass = new SampleClass ();
   sample.addEventListener(Event.COMPLETE , cComplete);

And you can use that variable in this class like:
  private fonction (e:Event):void {
    trace (e.target.variable);
}