School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 300,363 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,450 people online right now. Registration is fast and FREE... Join Now!




Load External Text in Flash CS3

 
Reply to this topicStart new topic

> Load External Text in Flash CS3

akozlik
Group Icon



post 31 May, 2008 - 06:27 PM
Post #1


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.

CODE

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.
Go to the top of the page
+Quote Post


Register to Make This Ad Go Away!

dfrydendall
*



post 2 May, 2009 - 11:19 AM
Post #2
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~>

QUOTE(akozlik @ 31 May, 2008 - 06: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.

CODE

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.

Go to the top of the page
+Quote Post

addsent
*



post 22 May, 2009 - 12:00 AM
Post #3
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
Go to the top of the page
+Quote Post

Monk1nr
*



post 9 Sep, 2009 - 02:25 PM
Post #4
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:
CODE
//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
Go to the top of the page
+Quote Post

Monk1nr
*



post 9 Sep, 2009 - 02:50 PM
Post #5
QUOTE(Monk1nr @ 9 Sep, 2009 - 02: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:
CODE
//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.
Go to the top of the page
+Quote Post


Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 


Lo-Fi Version Time is now: 11/7/09 08:24PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month