I'm creating a website using Adobe Flash Professional CS5.
I'm trying to read the content of a text file with URLLoader.load(path) inside a for loop.
The path changes every iteration.
When I trace the data from the event handler method, it returns only the last path's text file's content.
It seems like it's calling the event handler method only after the for has done looping.
CODE:
var myData:URLLoader = new URLLoader();
myData.addEventListener(Event.COMPLETE, onLoaded);
function onLoaded(e:Event):void
{
trace(myData.data);
}
for (var o = 0; o < filesArray.length; o++)
{
for (var p = 0; p < filesArray[o].length; p++)
{
if (filesArray[o][p] == "category.txt")
{
path = "C:\\inetpub\\wwwroot\\" + filesArray[o][0] + "\\" + filesArray[o][p];
myData.load(new URLRequest(path));
trace(path);
}
}
}
This is the output:
C:\inetpub\wwwroot\0001\category.txt
C:\inetpub\wwwroot\0002\category.txt
C:\inetpub\wwwroot\0003\category.txt
C:\inetpub\wwwroot\0004\category.txt
C:\inetpub\wwwroot\0005\category.txt
Jewlery
As I said, "Jewlery" is the content of "C:\inetpub\wwwroot\0005\category.txt".
I tried to change the "onLoaded" method to return a String like this:
function onLoaded(e:Event):String
{
return myData.data.toString();
}
That's inside the for loop:
trace(myData.load(new URLRequest(path)));
Then I got this as my output:
undefined
C:\inetpub\wwwroot\0001\category.txt
undefined
C:\inetpub\wwwroot\0002\category.txt
undefined
C:\inetpub\wwwroot\0003\category.txt
undefined
C:\inetpub\wwwroot\0004\category.txt
undefined
C:\inetpub\wwwroot\0005\category.txt
I also tried to convert the for loop to while loop, same result.
Why is it acting like that?
If URLLoader.load method doesn't work well inside a for loop, and I can do nothing about it, is there another way to read simple text from files, like FileStream or something? (I'm new to ActionScript, coming from C#)
Thanks in advance,
Freddy.