Groups | Blog | Home
all groups > flash actionscript > november 2005 >

flash actionscript : How to return an array from a function executed onLoad? (do I need a listener ... or a clue factory :)


Ken Fine
11/14/2005 7:46:26 PM
Hoping someone can help me out of the ninth circle of scoping hell. This
question may be basic: I've done a lot of actionscript-related reading, not
so much practical coding.

I'm using xfactorstudio's Xpath implementation to parse XML and to attempt
to return an array:

myDoc = new XML();
...
myDoc.onLoad = function(success){
arNodes = XPath.selectNodes(this,"/rss/channel/item/media:content/@url");
}
}

This works.If I attempt to loop through the array as with the code below, I
will get a collection of results:

myDoc = new XML();
myDoc.onLoad = function(success){
arNodes = XPath.selectNodes(this,"/rss/channel/item/media:content/@url");
for (var i = 0; i < arNodes.length; i++) {
trace("Element " + i + ": " + arNodes[i]);
}
}

If I attempt the same loop outside of the function body that's attached to
myDoc.onLoad, I return nothing. My array "arNodes" dies outside of the scope
of the inline function. I've tried various things to put the array on
timeline or global scope; these do not work. I've attempted to add a return
parameter, but there's a basic problem. That basic problem is that normal
attempts to cast a variable based on the output of a function are at odds
with executing the function with the success of myDoc.onLoad.

I am missing something basic, and it's driving me nuts. Perhaps I need to be
thinking about setting up a listener attached to onLoad, and executing
functions when the listener fires. At this point my brain is too knotted to
know heads from tails on this problem. Can someone show me the light?

Thanks,

-KF

sampurtill
11/15/2005 12:00:00 AM
Try this. State the variable arNodes at the top and then onec it loads, send it
to another frame and the variable will be sent with it.

var arNodes:Array;
myDoc = new XML();
myDoc.onLoad = function(success){
arNodes = XPath.selectNodes(this,"/rss/channel/item/media:content/@url");\
this.gotoAndStop(2);
}

On frame 2

for (var i = 0; i < arNodes.length; i++) {
trace("Element " + i + ": " + arNodes);
}

The only reason you can't call it outside of the onLoad function right now is
because it starts out as being undefined, so if you use the variable before the
XML loads, it's useless. So if you send it to frame 2 or another frame, it will
send the variable and all its data with it. I do this in all my flash apps.

Hope this helps,

Sam
sampurtill
11/15/2005 12:00:00 AM
You can also write a function to parse everything and call that function once
the XML loads, so

var arNodes:Array;
myDoc = new XML();
myDoc.onLoad = function(success){
arNodes = XPath.selectNodes(this,"/rss/channel/item/media:content/@url");
myFunction(arNodes);
}

function myFunction(array:Array){
for (var i = 0; i < array.length; i++) {
trace("Element " + i + ": " + array);
}

Try that too

Sam
}
Ken Fine
11/15/2005 9:38:19 AM
Thanks very much, sampurtill. Still working on my movie, but it's clear from
my trace statements that a variation on this second idea will work well.
Appreciate your help!

-KF


[quoted text, click to view]

sampurtill
11/15/2005 11:46:15 PM
No problem

AddThis Social Bookmark Button