external text

source: http://www.thegoldenmean.com

Load External Text into Flash MX, Page 2

The LoadVars() Object

The code on page one was pretty simple, but there are some big concepts at play here. I can’t be anything more than superficial about Object Oriented Programming in this tutorial. For now let us be content with some superficiality and accept that Flash MX comes with a slew of built in Objects we can customize for our own purposes. LoadVars is one of those Objects.

The code begins with the “new” keyword, standard syntax for creating a new instance of an Object and assigning that new instance to a variable name:

myData = new LoadVars();

That simple declaration creates a new instance of the built in LoadVars Object, and assigns it to the myData variable. (Of course you can name the variable anything you want for your specific project. For example, “newsData” might be much more appropriate for your own movie.)

Objects can have both methods and properties associated with them. Methods are the “verbs” — they are what an Object can do. Properties are the “nouns” — they are what an Object knows.

The Object you might be most familiar with is the MovieClip Object. What does a MovieClip know how to do? Have you ever used myClip.play(), or myClip.startDrag()? These are two examples of MovieClip Methods. What does a MovieClip know? It knows its position (_x, _y); it knows its transparency (_alpha); it knows its rotation (_rotation). These are examples of MovieClip Properties.

In our case, we’re going to use one Method of LoadVars (load), assign one property (two in later pages), and utilize on Event Handler (onLoad).

We begin by assigning a function to the onLoad event handler. Why? Why not just load the text and be done with it? The answer is because we don’t know how long it will take for the data to get from the server to the client. We know there will be some degree of latency in the delivery of the data, but how much is impossible to accurately estimate. In our trivial little example we have a very small text file loading from a hard drive. This occurs almost instantly. But in real life we might be loading much more text, and the delivery of the data will most likely be over an internet connection. This type of event is termed asynchronous — we don’t know how long it will take because it depends on things that are out of our control, including among other things the size of the .txt file, the speed of the user’s connection, traffic on the internet and the current load on the server. We need a mechanism to let us know when we have access to the variables we need, when it is safe to start working with the data.

With this in mind, we use the onLoad Event Handler. Flash waits until the variables are loaded and parsed, and then executes the onLoad handler which we have instructed to fire a custom function. Note that the onLoad Event Handler is placed before the Method (load). It is good practice to tell the script what to do with the data before it actually has the data!

Now — what are we telling Flash to do with the data once it has loaded?

myText_txt.text = this.content;

We have a text field on the stage whose instance name is “myText_txt”. The part of the script before the equals sign identifies that text field as our target. The single equals sign is the assignment operator. The part after the equals sign is interesting. The keyword “this” refers to the LoadVars Object we just created: “myData”. In OOP terms, the “scope” of the function is the myData Object. The myData Object now has a property, namely “content”. How did it get that property? We assigned it when we created the .txt document by beginning it with “content=”. As you might guess, the value of the property “content” is “This text has been brought to you courtesy of the LoadVars() object!”

Now in effect myData’s one property has been handed over to myText_txt, and thus appears in the dynamic text field.

The hard work is done. The next line simply tells the movie to go ahead and load the .txt file, now that it knows what to do with it. If you locate your text documents in a different directory (let’s say a directory named “texts”), this would be where you enter the path to that directory. In that case, your load statement might then appear this way:


You could stop here, because now you know the basics of everything you need to import a .txt file into Flash. But if you are interested, page three will discuss how to use a component to get a lengthier text file to scroll.

1 | 2 | 3 | 4

divider ornament