Solved

Actionscript 3.0 parse external text file

Posted on 2010-08-26
2
528 Views
Last Modified: 2013-11-11
I have a function that reads an external text file, and I have it able to display the file.  But I want to be able to display the contents of the text file ONE LINE at a time, with a delay between each line.. and during that delay, I want to be able to turn on an object and turn it off when the delay is done...

The following code does pretty much what I want, but the problem is it will stop on X number of characters, I want it to be able to stop on a word.. so that the entire word is displayed.

Any help would be appreciated.

-Joe
var url:String = texturl;
var t:int = 0;
var iMult:int = 1;
var timer:Timer = new Timer(2000);
var i:uint = 0;
var iLines:uint = 35;
var myLoader:URLLoader = new URLLoader();

myLoader.addEventListener(Event.COMPLETE, completeHandler);
myLoader.load(new URLRequest(url));

timer.start();
timer.addEventListener(TimerEvent.TIMER, gotime);
	
function gotime(e:TimerEvent)
{
	trace("Timer Event Called");
	while (t < (iLines * iMult))
	{
		trace("While Loop started");
		textbox_txt.appendText(str.charAt(t));
		t++;
		if(t>=str.length)
		{
			timer.stop();
		}
	}
	iMult++;
}

Open in new window

0
Comment
Question by:adworldmedia
2 Comments
 
LVL 10

Expert Comment

by:Carnou
ID: 33536632
I'd make use of the string.split() function.  If you want line at a time, it's simple (warning, untested code):
var arr:Array = str.split( "\n" );
for each (var s:String in arr)
{
    textbox_txt.appendText(s);
    textbox_txt.verticalScrollPosition = textbox_txt.maxVerticalScrollPosition; // this will make the scroll area follow your entry
}
I may have oversimplified, but that should be the general idea.  If you want word by word, change the split command to be str.split( " " );
Hope this helps!
0
 
LVL 1

Accepted Solution

by:
Adveho earned 500 total points
ID: 33741781

Now I didn't test this, but I think it's about right...
Atleast the general idea is. :)

Hope it helps. Good luck.

/*** Code start **/

var url:String = texturl;
var t:int = 0;
var iMult:int = 1;
var timer:Timer = new Timer(2000);
var i:uint = 0;
var iLines:uint = 35;
var myLoader:URLLoader = new URLLoader();
var arr:Array;

myLoader.addEventListener(Event.COMPLETE, completeHandler);
myLoader.load(new URLRequest(url));

/* This is missing? */
completeHandler(e:Event):void {
 // don't want the timer to start till the file is completely loaded.
 arr = e.currentTarget.split('\n');
  timer.start();
  timer.addEventListener(TimerEvent.TIMER, gotime);
}

      
function gotime(e:TimerEvent)
{
for(var i:int = 0; i < arr.length; ++i){
  if(i == iMult) {
 textbox_txt.appendText(arr[i]);      
 }
}
if(iMult == arr.length){
timer.stop();
}
      iMult++;
}
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I come across a lot of question about how to access things in the document class from a movieclip, or accessing something from a movieclip in the document class. It took me a while to figure this out but once I did it makes life so much easier. …
Sometimes you know that one object has a specific child in it, but you can't find the child. This happened to me when I was trying to code some actionScript to make a toolbar work with its embedded buttons.  My partner had created the toolbar usi…
The goal of the tutorial is to teach the user how to live broadcast using Flash Media Live Encoder and connecting it to YouTube to broadcast. Log into your Youtube account, choose live stream settings, start live stream from Flash Media Live Enc…
The goal of the tutorial is to teach the user how to how to record live broadcast.

756 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