Page 1 of 1

Load External Text in Flash CS3 Rate Topic: ***** 1 Votes

#1 akozlik  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 89
  • View blog
  • Posts: 797
  • Joined: 25-February 08

Post icon  Posted 31 May 2008 - 07:27 PM

I’ve been out of the Flash game for quite a while now. The last version I’ve been using was Flash MX. Imagine my surprise when I downloaded Flash CS3 and found that it now uses purely Actionscript 3.0. All my previous methods of programming Flash were rendered useless by all the new standards. Alas, I began my quest to find useful information, and hit a few roadblocks.

Adobe’s livedocs are ok, but it really helps to find real world examples. I tried searching for the various things I needed, but to no avail. I finally found some screencasts on youtube that helped, so I got my problem solved. This tutorial is what I've learned, and I hope it helps you get what you need done.

Now, enough of my Rant. Lets code!

So my main task for this article is to load an external file into a flash app, and display it in a text box. This is specifically good for loading text that will be changed periodically. Rather than open flash, edit the file, re-publish, and upload the new file, you can just pull all the data from a txt file and display it there. We can even render the text as HTML!

We’ll build this app using the TextArea component. Just go to Window->Components to pull up the components window. Drag a TextArea component to the stage, and give it an instance name. We’ll call ours external_faq.

Now for the code. I’ll give the code, and then I’ll go ahead and explain it. All of the following code goes in the timeline on the same frame as the objects it references.

var faqLoader:URLLoader = new URLLoader(); // Step 1
faqLoader.load(new URLRequest(”test.txt”)); // Step 2

faqLoader.addEventListener(Event.COMPLETE, onComplete); // Step 3

function onComplete (event:Event):void // Step 4
{
var faq:String = event.target.data; // Step 5
external_faq.text += faq; // Step 6
}



Step 1
We need to make a new URLLoader, so we define the variable faqLoader as a URLLoader, then call a new URLLoader() object. The URLLoader will hold the data returned by the URL.

Step 2
We go ahead and load the url, using the URLRequest() object. We use the load method of the URLLoader object, and open a new URLRequest object, which contains the path to our text file. Simple enough.

Step 3
Now we go ahead and add an Event Listener to our faqLoader to listen for when the loading as been complete. An event listener is the equivalent of using an “onWhatever” event with older Action Script. There are many different Event Listeners and Event types. For instance you could have an event:MouseEvent and check for when MouseEvent.CLICK occurs to see when a user clicks their mouse on the object the code is attached to. That’s getting on a tangent though.

Step 4
We set a String variable to hold the text that is loaded from the file. This will enable us to do any string manipulation we may wish to do. However, we’re going to keep this one simple, and just display the text to the TextArea component.

Step 5
Finally, we set the text property of the TextArea component to our String variable. Note that we I am not using external_faq.text = faq. I am using external_faq.text += faq. Also, if you wanted to use html formatted text, you would use the htmlText property.

Finally, the text file that you have written doesn’t need any variables inside it. All it needs to contain is the text you wish to display.

I hope this tutorial is useful for anybody who had the same trouble I did. I know it’s a relatively simple application, but making that migration from Actionscript versions can be tough. Question and comments are welcome. I'm by no means a Flash expert, but I'll see what I can answer. I'm sure others will as well.

Is This A Good Question/Topic? 0
  • +

Replies To: Load External Text in Flash CS3

#2 dfrydendall  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 02-May 09

Posted 02 May 2009 - 12:19 PM

I'm new to Cs3 as well and I feel like I'm starting over from scratch.
My question is: You mentioned the htmlText property. How would that fit into the scripting? My goal is to create the old "The client needs to update the site text in HTML." with 3 different external files for 3 different text areas.

Any help would be appreciated since now I'm almost out of hair due to hair pulling frustration.

Thanks!

DF~>

View Postakozlik, on 31 May, 2008 - 06:27 PM, said:

I’ve been out of the Flash game for quite a while now. The last version I’ve been using was Flash MX. Imagine my surprise when I downloaded Flash CS3 and found that it now uses purely Actionscript 3.0. All my previous methods of programming Flash were rendered useless by all the new standards. Alas, I began my quest to find useful information, and hit a few roadblocks.

Adobe’s livedocs are ok, but it really helps to find real world examples. I tried searching for the various things I needed, but to no avail. I finally found some screencasts on youtube that helped, so I got my problem solved. This tutorial is what I've learned, and I hope it helps you get what you need done.

Now, enough of my Rant. Lets code!

So my main task for this article is to load an external file into a flash app, and display it in a text box. This is specifically good for loading text that will be changed periodically. Rather than open flash, edit the file, re-publish, and upload the new file, you can just pull all the data from a txt file and display it there. We can even render the text as HTML!

We’ll build this app using the TextArea component. Just go to Window->Components to pull up the components window. Drag a TextArea component to the stage, and give it an instance name. We’ll call ours external_faq.

Now for the code. I’ll give the code, and then I’ll go ahead and explain it. All of the following code goes in the timeline on the same frame as the objects it references.

var faqLoader:URLLoader = new URLLoader(); // Step 1
faqLoader.load(new URLRequest(”test.txt”)); // Step 2

faqLoader.addEventListener(Event.COMPLETE, onComplete); // Step 3

function onComplete (event:Event):void // Step 4
{
var faq:String = event.target.data; // Step 5
external_faq.text += faq; // Step 6
}



Step 1
We need to make a new URLLoader, so we define the variable faqLoader as a URLLoader, then call a new URLLoader() object. The URLLoader will hold the data returned by the URL.

Step 2
We go ahead and load the url, using the URLRequest() object. We use the load method of the URLLoader object, and open a new URLRequest object, which contains the path to our text file. Simple enough.

Step 3
Now we go ahead and add an Event Listener to our faqLoader to listen for when the loading as been complete. An event listener is the equivalent of using an “onWhatever” event with older Action Script. There are many different Event Listeners and Event types. For instance you could have an event:MouseEvent and check for when MouseEvent.CLICK occurs to see when a user clicks their mouse on the object the code is attached to. That’s getting on a tangent though.

Step 4
We set a String variable to hold the text that is loaded from the file. This will enable us to do any string manipulation we may wish to do. However, we’re going to keep this one simple, and just display the text to the TextArea component.

Step 5
Finally, we set the text property of the TextArea component to our String variable. Note that we I am not using external_faq.text = faq. I am using external_faq.text += faq. Also, if you wanted to use html formatted text, you would use the htmlText property.

Finally, the text file that you have written doesn’t need any variables inside it. All it needs to contain is the text you wish to display.

I hope this tutorial is useful for anybody who had the same trouble I did. I know it’s a relatively simple application, but making that migration from Actionscript versions can be tough. Question and comments are welcome. I'm by no means a Flash expert, but I'll see what I can answer. I'm sure others will as well.

Was This Post Helpful? 0
  • +
  • -

#3 addsent  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 39
  • Joined: 02-January 09

Posted 22 May 2009 - 01:00 AM

Sir,
i m want to know about flash and action script.
Even u have any .pdf learning book about this subjects for beginer.
please send me my emailid :suraj_singh61@ibibo.com
Was This Post Helpful? -1
  • +
  • -

#4 Monk1nr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-September 09

Posted 09 September 2009 - 03:25 PM

Ok, I am trying to do this code and getting the error: 1046: Type was not found or was not a compile-time constant: EVENT.

code is here:
//section for loading external text information
var contentLoader:URLLoader = new URLLoader();
contentLoader.load(new URLRequest("story.txt"));

contentLoader.addEventListener(Event.COMPLETE, onComplete);

function onComplete(event:EVENT):void
{
	var txt:String = event.target.data;
	myText.text += faq;
}
											   
											


myText is and instance of the TextArea that is in my library.

I tried adding import flash.events.Event but that didn't work either.

I am using Flash CS3 with ActionScript 3.0

Thanks, Corey
Was This Post Helpful? 0
  • +
  • -

#5 Monk1nr  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 09-September 09

Posted 09 September 2009 - 03:50 PM

View PostMonk1nr, on 9 Sep, 2009 - 02:25 PM, said:

Ok, I am trying to do this code and getting the error: 1046: Type was not found or was not a compile-time constant: EVENT.

code is here:
//section for loading external text information
var contentLoader:URLLoader = new URLLoader();
contentLoader.load(new URLRequest("story.txt"));

contentLoader.addEventListener(Event.COMPLETE, onComplete);

function onComplete(event:EVENT):void
{
	var txt:String = event.target.data;
	myText.text += faq;
}
											   
											


myText is and instance of the TextArea that is in my library.

I tried adding import flash.events.Event but that didn't work either.

I am using Flash CS3 with ActionScript 3.0

Thanks, Corey



Never mind. This is a prime example of my first rule of programming: It's ALWAYS the dumb$%^*. Number two and it's a close second, is ALWAYS have someone else look at your code, because (see rule number one) dumb$%^&* is hard to notice after staring at your code and perceiving nothing wrong.

it is not event:EVENT but event:Event.
Was This Post Helpful? 0
  • +
  • -

#6 rgd713  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 19-January 10

Posted 19 January 2010 - 02:24 PM

I get the following 2 errors when publishing, I am working on CS4, but publishing to AS2.0-

The class or interface 'URLLoader' could not be loaded.
var faqLoader:URLLoader = new URLLoader(); // Step 1

and

The class or interface 'Event' could not be loaded.
function onComplete (event:Event):void // Step 4

my code is (copied directly from the tutorial):

var faqLoader:URLLoader = new URLLoader(); // Step 1
faqLoader.load(new URLRequest("search_result.txt")); // Step 2

faqLoader.addEventListener(Event.COMPLETE, onComplete); // Step 3

function onComplete (event:Event):void // Step 4
{
var faq:String = event.target.data; // Step 5
external_faq.text += faq; // Step 6
}

Please help!!
Was This Post Helpful? 0
  • +
  • -

#7 SDragon029  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 01-September 09

Posted 27 January 2010 - 10:03 AM

View Postrgd713, on 19 Jan, 2010 - 01:24 PM, said:

I get the following 2 errors when publishing, I am working on CS4, but publishing to AS2.0-

The class or interface 'URLLoader' could not be loaded.
var faqLoader:URLLoader = new URLLoader(); // Step 1

and

The class or interface 'Event' could not be loaded.
function onComplete (event:Event):void // Step 4

my code is (copied directly from the tutorial):

var faqLoader:URLLoader = new URLLoader(); // Step 1
faqLoader.load(new URLRequest("search_result.txt")); // Step 2

faqLoader.addEventListener(Event.COMPLETE, onComplete); // Step 3

function onComplete (event:Event):void // Step 4
{
var faq:String = event.target.data; // Step 5
external_faq.text += faq; // Step 6
}

Please help!!


Hey,
I just tried the code in CS4 as well and got it to work but it was an AS3 document when I tried to switch it over to an AS2 document I got the same errors as you did. You would be getting those errors for the plain and simple explanation being your trying to use AS3.0 code in an AS2.0 environment. That would be why those errors are popping up for yeah. As for doing this in AS2.0 I am not sure since I didn't start messing around with code like this until AS3.0. So good luck on finding a solution to this in AS2.0 or you can use this code but convert your other code to AS3.0 standards.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1