views:

897

answers:

3

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!

+1  A: 

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...
}
clownbaby
Hi, I replied to the other answer explaining further what I want to do so I'll reply to you too! :) "okay I put together a basic example of what I want to achieve, it is available on pastebin.com: http://pastebin.com/d64fe9e06 Does this show what I want to achieve better? I want to get the resulting array from "convertXML" to be returned by "loadinformation" so "returnedArray" = the array from convertXML. The code is probably error containing, but it wasn't tested, just made as an example of what I want to achieve."
+2  A: 

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!

Tyler Egeto
Hi, I'm aware of this, I think. My problem is that I need to get back the Array from the function I'm calling, everything I've tried from the way I understand it (the way you explained) hasn't worked, I don't seem to be able to take the array from the function used (in your example "handleLoadComplete") to the main stage. Does this make sense? That's my problem, I need to be able to do this but I can't find a way with my minimal understanding of AS3, so I tried out what I posted and hoped it might be the right way... Thanks!
The answers provided are correct. If you are still having issues, be specific about what issue you are having. Do you mean that you are now able to generate your array, but don't know how to pass it to another function / scope? Where are you trying to "use" the data? A bit more explanation of your setup, and sample code might help.
sberry2A
What you are attempting is not possible. In your handler function, you will need to assign it to the desired array.
Tyler Egeto
I have a function called "loadinformation", inside that function I have "LoadXML" and "convertXML". I provide "loadinformation" with an XML url, that then does some stuff and gets the XML data, it then passes the XML data to LoadXML which does some stuff and then it passes that data to convertXML which produces my final data.LoadXML returns an array, I need this Array to be accessible by the "loadinformation" function. loadinformation(xmlurl) -> LoadXML -> convertXML -> returns array.Does this make sense? I can draw a diagram if needed, I'm not very good at explaining.
okay I put together a basic example of what I want to achieve, it is available on pastebin.com: http://pastebin.com/d64fe9e06Does this show what I want to achieve better? I want to get the resulting array from "convertXML" to be returned by "loadinformation" so "returnedArray" = the array from convertXML.The code is probably error containing, but it wasn't tested, just made as an example of what I want to achieve.
A: 

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;
}
Selene
Hi, this is what I've been trying. I get to the convertXML function and trace my array (trace(rArray)) and it works fine, however if I then assign "returnedArray" with "returnedArray = convertXML(xmlData);" (inside LoadXML) and trace it I get nothing. For some reason returnedArray is not getting the array from convertXML.Any ideas why this might be?
Wait nevermind it does work. If I trace out returnedArray after doing convertXML(xmlData); it gives me the results, but if I do "return returnedArray" AFTER the convertXML function is declared (eg: bottom of my code) it returns "Null". At what point should I be returning "returnedArray" to be able to use the final result?
returnedArray will only contain data *after* LoadXML is called, which happens when the XML file finishes loading. What is the rest of the code like?If you want more things to happen when returnedArray gets filled with data, you have three options: 1) Add more things to LoadXML. 2) Create a second function with the additional things, and also add it as an event listener to xmlLoader. 3) At the end of LoadXML, dispatch an event saying the data is loaded.I can expand on any of the three you wish.
Selene
Thanks for your help so far. Do you see how in your code returnedArray is set in "LoadXML"? Well I want to be able to access that resulting array (returnedArray) from the parent function and return it for further usage. http://pastebin.com/m6cb9a430 This is an example of what I can achieve. My problem is that I can't just "return returnedArray" after loadInformation because LoadXML is called by an event handler and I have no way of creating an array from the results of that.Does this make sense?
Okay, what are you trying to do with the array of data from the XML? Either put that code in LoadXML, or put it in a function that you call from LoadXML.
Selene