AddThis Social Bookmark Button

You will have noticed that the parameter success is passed with the function associated to the onLoad event. The parameter success is a Boolean value that indicates whether the load operation ended in success (true) or failure (false). Therefore in our little example if success is equal to true it will display
My name is Robert, I am 86 and I live on Mars. if not it will display there was a problem loading the variables.

onData and decode

onData : "Invoked when data has completely downloaded from the server or when an error occurs while data is downloading from a server. This handler is invoked before the data is parsed and can be used to call a custom parsing routine instead of the one built in to Flash Player. The value of the src parameter passed to the function assigned to LoadVars.onData can be either undefined or a string that contains the URL-encoded name-value pairs downloaded from the server. If the src parameter is undefined, an error occurred while downloading the data from the server." (ActionScript 2.0 Language Reference)

If you use onData and onLoad in the same code like below, you will notice that onLoad won't be called. The default implementation of LoadVars.onData invokes LoadVars.onLoad. This default implementation is overridden by assigning a custom function to LoadVars.onData which is our case here.

myVariables = new LoadVars();

myVariables.load("myFile.txt");

myVariables.onLoad = function(success){
if(success){
trace("I won't show ");
} else {
trace("I won't show either");
}
}

myVariables.onData = function(src){
trace("hello from onData");
trace(src)
}

// will display "hello from onData"


The parameter src contains the raw data loaded from the text file it will either be a string of raw data or undefined. onData is called before the data is actually parsed, which literally means before the variables are converted into properties of the myVariables object. Therefore the following code will return undefined

......

myVariables.onData = function(src){
trace(myVariables.myName);
}

// will display "undefined"


If you want to convert the raw data into properties of the myVariables object you need to use the decode method like in the code below.

......

myVariables.onData = function(src){
myVariables.decode(src)
trace(myVariables.myName);
}

// will display "Robert" because the raw data has been converted into properties of myVariables



other useful methods of the LoadVars class


getBytesLoaded() : Returns the number of bytes downloaded by LoadVars.load() or LoadVars.sendAndLoad(). This method returns undefined if no load operation is in progress or if a load operation has not yet begun. (ActionScript 2.0 Language Reference)

getBytesTotal() : Returns the total number of bytes downloaded by LoadVars.load() or LoadVars.sendAndLoad(). This method returns undefined if no load operation is in progress or if a load operation has not started. This method also returns undefined if the number of total bytes can't be determined (for example, if the download was initiated but the server did not transmit an HTTP content-length). (ActionScript 2.0 Language Reference)

The two methods above can be very useful when you have large amount of data to load and you need to provide the user with information regarding the current state of the loading process.

This is just a basic tutorial that will hopefully help you to understand the LoadVars object a bit better, more info on the LoadVars object can be found at Adobe.com

Good luck :)





download source filesdownload source files
AddThis Social Bookmark Button
If you think this page is providing useful information, don't hesitate to leave a comment.
flashvalley
 
Copyright ©2006-2008 flashvalley.com - All rights reserved