
Page 3 of 3
Jesse Stratford
Jesse lives and works in Melbourne Australia. He is the Cofounder of http://ActionScript.org. A Flash enthusiast, teacher, author, freelancer and speaker Jesse's main focus nowadays is managing http://ActionScript.org, but he enjoys participating actively in community and the wider Flash scene when he has time.
View all articles by Jesse StratfordThe Flash MX Extension, LoadVars Objects:
Macromedia, being the darlings they are, provided us with a nifty extension of loadVariables in Flash MX in the form of the new LoadVars Object. A LoadVars Object is basically just a class which implements the loadVariables operations and a few other things automatically, with built in methods to detect when the text is loaded and built in event handler allowance for the new Flash event model (see FlashGuru's tutorial).
If that paragraph scared you, come back. I'm just being a geek; you'll see what I mean in a moment. A new LoadVars object is created using the ‘new' operator, just like all other Objects in Flash:
myLoadbleData = new LoadVars();
Data is then loaded in using the load() method.
myLoadbleData.load("file.txt");
Once you've created a new LoadVars object you can get into using the object's built in methods. For instance, you can use the following code to determine the percentage of the text which has loaded so far:
percentLoaded = myLoadbleData.getBytesLoaded() / myLoadbleData.getBytesTotal();
As you can see, these methods save us having to ‘swonk' our data. Further you can create a function which will automatically be called when the variables are all loaded, which is better still. That's done in the following manner:
myLoadbleData.onLoad = function (success) {
if (success) {
// Call your parser here perhaps
} else {
// The data didn?t load at all. Display error
}
}
Note that the above code includes an argument called ‘success'. This is passed in by Flash when the onLoad function is called and is either true or false. True indicates that the load operation was successful. Load actions most commonly fail when the filename you specified is wrong.
A few important things to consider about LoadVars Objects include:
- Your data is loaded into the Object you created, not into a target MC or level as before, so the path to your variables becomes yourLoadVarsObjectName.yourVariableName .
- LoadVars has great built in support for sending and accepting data which is beyond the scope of this tutorial. Read up in Macromedia's documentation if you're interested.
- Everything is still a String, so remember to convert data types within Flash where appropriate.
Considerations:
A client recently came to me wondering why their content didn't work in the same manner when they loaded it from the web as it did when they loaded it from their local disk. Specifically, the issue was that they had intentionally included newlines in the text file they were loading because they observed it made a nice new paragraph in their text box in Flash. Until they uploaded it. The reason for this is that different operating systems use different notations for the various types of white space. A newline on UNIX/Linux box (which most web servers are) is represented differently to the same character on a Windows machine. The moral of the story is don't rely on carriage returns and paragraph breaks within your text file. If you need to implement such things in the text file, use a special character like "|" wherever you want a newline to occur in your text file, then when you load your content into Flash and parse it, replace all occurrences of "|" with "\n" (the newline character), or similar.
Phew. 2500 words! It's amazing how you can procrastinate when you should be doing Java projects for University. As always, comments, corrections and constructive criticism are welcome. Cheers
| Jesse Stratford is the Co-Master of ActionScript.org and a freelance Flash developer and teacher. He is based in Australia and enjoys all things Flash. NB: If you have comments or feedback please feel free to email me, but please do not email me Flash questions; the forums are provided for that purpose and you will get a faster answer by posting you question there. |
If you have found this tutorial helpful, I hope that you will take 30 seconds to visit The Hunger Site where, with just one click you can make a free donation of food to a starving person in a third-world country. We do not benefit financially from this action; it is purely an act of charity. |
| This tutorial is protected by International Intellectual Property Rights laws and may not be reproduced or redistributed in full or part, without the prior written consent of the author. Unauthorized reproduction of this tutorial or its contents may result in prosecution. I've worked hard on this tutorial, please don't steal it. |


