?
Solved

ActionScript 3 Load Text into array and call from outside function

Posted on 2012-09-08
3
Medium Priority
?
1,144 Views
Last Modified: 2012-09-09
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
0
Comment
Question by:bedsingar
  • 2
3 Comments
 
LVL 37

Accepted Solution

by:
CyanBlue earned 1500 total points
ID: 38380167
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
 

Author Comment

by:bedsingar
ID: 38380499
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
 

Author Closing Comment

by:bedsingar
ID: 38380503
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
This article discusses how to implement server side field validation and display customized error messages to the client.
The goal of the tutorial is to teach the user how to set there setting in Adobe Flash Media Live Encoder and YouTube for optimal video and audio quality.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
Suggested Courses

621 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question