Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Actionscript 3.0 parse external text file

Posted on 2010-08-26
2
527 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: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

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

Introduction This article is primarily concerned with ActionScript 3 and generally specific to AVM2.  Most suggestions would apply to ActionScript 2 as well, and I've noted those tips that differ between AS2 and AS3. With the advent of ActionS…
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. …
In this tutorial viewers will learn how to create a basic motion tween animation in Flash Open a new document in Flash: Draw/import an image: Press CTRL + F8 to convert it into a graphic symbol: Select a frame (how long you want the tween to last): …
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…

809 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