ActionScript 3 Load Text into array and call from outside function

Hello,

I'm trying to load text values into an action script array, but I can't figure out once the array is loaded how to call the elements of that array from outside the load function.

here's the code at the moment:

var csvLoader:URLLoader = new URLLoader(new URLRequest('boxtext.txt'));
csvLoader.addEventListener(Event.COMPLETE, csvLoaded);


function csvLoaded(event:Event):void{
   var lines:Array = String(event.target.data).split('\n');
   var linesNum:int = lines.length;
   for(var i:int = 0 ; i < linesNum; i++){
     trace('line ' + i + ': ' + lines[i]);
   }
}

Open in new window


boxtext.txt is just a file with text descriptions on separate lines i.e:

Description 1
Description 2
Description 3

I'd like to be able to call each of the elements using a loop. I can see how to do this if within the function, but I'd like to call the elements from another function.

I've tried defining the array outside of the function (which works for variables), but doesn't seem to work for an array. But that said I'm not sure I'm populating the array correctly taking this approach...

var csvLoader:URLLoader = new URLLoader(new URLRequest('boxtext.txt'));
csvLoader.addEventListener(Event.COMPLETE, csvLoaded);
var lines:Array

function csvLoaded(event:Event):void{
   lines = String(event.target.data).split('\n');
   var linesNum:int = lines.length;
   for(var i:int = 0 ; i < linesNum; i++){
     trace('line ' + i + ': ' + lines[i]);
   }
}

Open in new window

 

The examples I've found online so far only show how to load the data - not how to call it from there... so I'm probably missing something basic.

Any help would be appreciated.

Thanks
bedsingarAsked:
Who is Participating?
 
CyanBlueCommented:
I'm probably missing something here as well.  The script looked okay for me so I even tried myself and it did work for me. You should be able to access that lines array as long as you access it after it is populated.

Now, I am scratching my head. What is your question?

CyanBlue
0
 
bedsingarAuthor Commented:
CyanBlue,

I've updated the board from the last question you answered for me ... EE STUFF LINK - URLLoader

As you can see from the full script I'm trying to call elements of the array to replace the board text that was previously set up as demo text while I was trying to solve the token issues you saw previously.

function mouseOver(event:MouseEvent):void
{
	var index:uint = event.currentTarget.tabIndex;
	
//	trace(index + " + "+ event.currentTarget);
	//  BoardText.text = ("Target " + index + " Hit") ;
	BoardText.text = (lines[index - 1]) ;

}

Open in new window



This throws up an error though:

Scene 1, Layer 'AS3', Frame 1, Line 128      1120: Access of undefined property lines.

The function runs on load, so I assume that I don't have to call it separately? I've tried adding csvLoaded; before the function, but that doesn't make any difference.

Thanks
0
 
bedsingarAuthor Commented:
Scratch that - I can see that you meant my second attempt at the code was correct! ....

I've switched back to that and tried calling the elements and that works fine - I was obviously just being an idiot last night!

Thanks Again
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.