Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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!

like image 252
sam Avatar asked May 24 '26 14:05

sam


2 Answers

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!

like image 113
Tyler Egeto Avatar answered May 27 '26 05:05

Tyler Egeto


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...
}
like image 37
danjarvis Avatar answered May 27 '26 07:05

danjarvis