开发者

AS3: return array from event listener?

I have an event listener applied to an xml load and it currently traces out the values it grabs which is fine, but what I want it to do is return an array for me to use. I have the Array creation and return working from "LoadXML" (it returns the array) but I can't get this to work with an event listener.

The event listener runs the "LoadXML" function fine, but I have no idea how to take the returned array for use, this is an example of how my event listener works right now:

xmlLoader.addEventListener(Event.COMPLETE, LoadXML());

and my assumption of how I 开发者_高级运维would take the array (this doesn't work):

var rArray:Array = xmlLoader.addEventListener(Event.COMPLETE, LoadXML());

so instead I tried the following:

xmlLoader.addEventListener(Event.COMPLETE, function():Array{
    var rData:Array = LoadXML(datahere);
    return rData;
}

but that doesn't worth either.

So: How do I return an array from an eventlistener? Thanks!


I think there is some confusion of how event listeners work. Actually, I'm surprised your not getting compile errors with your current code.

When adding an event listener, what you should be passing in is a reference to a function to be called at a later time. Then when that function gets called, it will pass an Event object with any retrived data for working with. Here is a example:

xmlLoader.addEventListener(Event.COMPLETE, handleLoadComplete/*Note: No brackets, this is a reference*/);

//will be called at a later time, not instantly.
function handleLoadComplete(e:Event):void {
    var xml:XML = xmlLoader.data as XML;
    //do what ever you want this the XML...
}

Hopefully that makes things clearer for you.

Happy Coding!


Why not just use a component-level object and set its value (xml contents in your LoadXML() method)?

var rArray:Array;
xmlLoader.addEventListener(Event.COMPLETE, LoadXML);

private function LoadXML(event:Event=null):void {
    // set this.rArray in here...
}


It's possible to make returnedArray contain an array created by convertXML, but not in the way you're trying to do it. Flash simply doesn't work that way.

This is roughly what happen when you run the code from pastebin:

  1. Start running function loadInformation()
    • var returnedArray:Array = loadinformation("http://website.com/xml.xml");
  2. Tell Flash that when xmlLoader loads completely, it should run LoadXML()
    • xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
  3. Start loading an XML file
    • xmlLoader.load(new URLRequest(xmlurl));
  4. Tell Flash what LoadXML() is (and convertXML())
    • function LoadXML(e:Event):void {...}
  5. Stop running function loadInformation()
  6. Flash goes off and does other stuff while waiting for the XML file to load
  7. The XML file finishes loading. Flash calls LoadXML() like it was told to.
    • Note that LoadXML() is being called by Flash, not by loadInformation().
  8. LoadXML() processes the XML file.

To get the converted array data, you need to do something like clownbaby's answer: set the value of returnedArray directly while inside LoadXML.

var returnedArray:Array;

loadinformation("http://website.com/xml.xml");

function loadinformation(xmlurl:String):Array{
    var xmlLoader:URLLoader = new URLLoader();
    var xmlData:XML = new XML();
    xmlLoader.addEventListener(Event.COMPLETE, LoadXML);
    xmlLoader.load(new URLRequest(xmlurl));
}

function LoadXML(e:Event):void {
    xmlData = new XML(e.target.data);
    returnedArray = convertXML(xmlData);
}

function convertXML(xml):Array{
    // Does some stuff with the XML and returns an array
    return rArray;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜