Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
In this movie we'll learn how to fill a text field with text from an external text file. If you'd like to follow along, we're in External.fla in the chapter 08 folder. Now you may be wondering, why would I ever want to fill a text field with external text? If you're a designer or if you've ever done any work in any kind of design program at all, and you presented the work to your client or your boss or whoever, and they see it and they say, you now what it's great except for I want you to change this little thing or this little thing or just make some change in a word in a text field, and then you have to go in your Flash file and hunt it down wherever that text field was.
It can be really tedious and kind of frustrating. If you store your text in an external text file you can have whoever is criticizing your file fix it themselves. That makes life a lot easier. By using an external text file, your work becomes a lot more efficient. So let's take a look at what we have right now. Let's look at the first keyframe of our Actions panel and open up the Actions window by pressing Option+F9 on the Mac or F9 on the PC. So we have a variable called external_txt, the datatype is a text field equal to a new instance of the text field class.
The x is 175. The y is 100, It has a border. The width is 200, and the height is 200 and we put it on the Stage with addChild. So let's test the movie. And we have a text field in the center of the Stage. Now there are few steps in order to get an external file. So the first thing that we have to do is create something called a URL request.
After the first line, let's click. Go to the next line. Type var and then a space, and then I'm going to call this externalReq, for external request. You might remember in the events chapter, We talked about how to link to a web site and we used the URL request. That's what we're going to use here. So type a colon, then type capital U, capital R, capital L, and then R for request. Type space, equals, space, new, space, URL, all capitals and capital R and spell out Request, open parenthesis.
Now it's asking for a URL that's a string, so type open quote, and inside of the same folder there's a file called external, so type that in .txt, it's a plain text file. Type a close quote, close parenthesis and a semicolon. Let's go to the next line. Create a new variable called externalLoad with a capital L. So type a colon, and now we're going to create something called the URL loader to load this URL. So type URL, and then loader with a capital L, and type space, equals, space, new, URLLoader with a capital L (); This creates a new instance of the URLLoader class and our loader or URLLoader is going to load our URL request.
Let's go down a few lines and then tell our loader to load our request. So type externalLoad with a capital L, .load, then type an open parenthesis, and now you type in a request. The name of our request object is externalReq. So let's type in externalReq, capital R, close parenthesis and a semicolon.
Now we want to do is add an event listener so that when the load is complete or when this text file is finished loading and ready to use, thne we can put it inside of our text area. So let's go down a few lines. Type externalLoad with a capital L, .addEventListener with a capital E and a capital L, open parenthesis. The event type is event with capital E, .COMPLETE and that's all capital.
That means when this is URL, external.txt has finished loading and is ready to use then type a comma and a space. Then we can call this event handler textReady with a capital R. Close parenthesis and a semicolon. Let's define the textReady event handler at the bottom of our code. I'm going to click after the addChild on line 15. Go down a few lines and type function and a space, textReady with a capital R, open parenthesis.
This will receive an event, all lowercase, colon, the datatype is Event with a capital E, close parenthesis. When the load is complete, the information will be sent to this event handler. After the parenthesis, type a colon then void. Go down to the next line, open curly brace, next line, close curly brace, up arrow, Enter. Now we're ready to put that text inside of the text field. So now type external_txt.text, space, equals, space.
Now we'll set the text property of the external text field to event.target, that's the loader, and then .data; event is the complete. event.target is referring to listener that listens for that event, which would be external load and the data property of external load is equal to this external text file or the data inside of that external text file.
Test the movie. Might have to move the Actions panel a little bit. Notice that there are a few things that are kind of annoying about this. It scrolls but it only scrolls to the right, and so each paragraph is on a single line, which is kind of ridiculous. We'll learn how to control the scroll in the next movie, but for right now, let's just make it word wrap so that the paragraphs will look right.
So close that window. On line 13 after it says external_txt.height, click after the; press Enter. Now we just need to set the word wrap property and this will cause the words to wrap around instead of going infinitely to the right until there's a new paragraph. So type external_txt.wordWrap. This will cause the words to wrap around. Make sure the W in Wrap is capitalized. space, equals, space, true; Test the movie.
There we go. If you click inside the text field and drag down, and it will scroll. If you wanted to style this text field, you could easily do that. You just apply to text format in the same way that we did it earlier this chapter. So that is how you load data from an external text file. Just remember to create the URL request. That's going to hold the information about where your text file is. You load it with a URL loader. Then you to handle using event.complete, and then you use event.target.data to place the data inside of the text field.
Now that you know how to load text, let's make the text scroll in the next movie.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101672 Viewers
61 Video lessons · 88422 Viewers
71 Video lessons · 72266 Viewers
56 Video lessons · 103990 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.