Return to Kroll Design home page

Flash Tutorials (with .FLA files included)

 
« Return

ActionScript 2

Using the LoadVars class to read an external plain text file of variable name/value pairs

Get Adobe Flash player

The Flash "LoadVars" class allows you to put name-value pairs into an external text file, and the Flash movie reads them in when the Flash movie plays. In this example, the content for banner announcements is read in, as is the desired duration of each message. The message content is placed into a dynamic text field which is part of a movie clip that in this case is moved from right to left with a blur effect, paused for the desired duration to allow the user to read the message, then moved off screen to allow the process to be repeated with the next message.

Here is the external text that is read in:

msg1=This is message 1. It has a duration of 6 seconds. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor lorem vel augue placerat sed condimentum lacus.&msg1_duration=6&msg2=This is message 2. It has a duration of 4 seconds.&msg2_duration=4&msg3=Message 3. Duration 2 seconds.&msg3_duration=2&msg4=This is message 4. It has a duration of 7 seconds.&msg4_duration=7

To make the text easier to edit, you can add line breaks while editing it:

msg1=This is message 1. It has a duration of 6 seconds. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Quisque auctor lorem vel augue placerat sed condimentum lacus.
&msg1_duration=6

&msg2=This is message 2. It has a duration of 4 seconds.
&msg2_duration=4

&msg3=Message 3. Duration 2 seconds.
&msg3_duration=2

&msg4=This is message 4. It has a duration of 7 seconds.
&msg4_duration=7


When you are done editing the text, remove the line breaks, so that the Flash movie can successfully read the text in. Also note that an ampersand is required between name-value pairs, as shown.

External ActionScript can be read it at compile time using the "#include" directive, when the movie is published. But the LoadVars technique is more convenient for when end users need to make updates without having to open the .FLA file in Flash.

Notes about the ActionScript code:

To test for whether the load was successful, the documentation says to query a variable called "success", but you can call it anything you want.

It's not worth it to attempt reading in more than one text file at once, it gets too complex and easy to break. The files might finish loading in an order other than the order you want, which can mess up the sequence of elements in an array if you're putting the loaded files into an array. So it's recommended that you read just one text file.

This example includes code that can handle missing components of messages 1 through 4, and can even handle the external file missing altogether. When it encounters a "not found" condition, it either skips displaying a message, or it displays a default message.

Download the FLA file

Download the external text file

// THIS IS FRAME 1 of 3 OF THE MAIN TIMELINE

stop();

var msg_array = new Array();
var msg_duration_array = new Array();

var duration:Number;
var default_duration:Number = 15;
var default_msg:String = "This is the default message. Either no text file was found, or, no message content within it.";

myLoader = new LoadVars();
myLoader.load("movies/external_content/messages.txt");
myLoader.onLoad = function(success) {	
	if(success) { 
	   msg_array[0] = this.msg1;
	   msg_array[1] = this.msg2;	
	   msg_array[2] = this.msg3;	
	   msg_array[3] = this.msg4;		   	   
	   msg_duration_array[0] = this.msg1_duration;
	   msg_duration_array[1] = this.msg2_duration;	  
	   msg_duration_array[2] = this.msg3_duration;	  
	   msg_duration_array[3] = this.msg4_duration;
	   gotoAndStop(2);	   
	   }
	else { 
	   msg_array[0] = default_msg;
	   msg_duration_array[0] = default_duration;
	   gotoAndStop(2);
	   }   
}
// THIS IS FRAME 2 of 3 OF THE MAIN TIMELINE

stop();

var number_of_msgs:Number = msg_array.length;

var current_msg:Number = 0;
var time_left:Number = msg_duration_array[0];

/* When the msg_array element is empty (null / undefined),
   remove array elements from both msg_array and msg_duration_array.
   In the FOR loop, work from the end of the array towards the beginning,
   so that the 'splice' command doesn't allow a subsequent bad array element to 
   sneak into the newly vacated array slot undetected:
*/
for(i=(number_of_msgs-1); i>=0; i--)
{  
  if (msg_array[i] == null)
  { msg_array.splice(i,1);
    msg_duration_array.splice(i,1);
  }
}

/* Now that some removal of array elements was potentially done,
   recalculate 'number_of_msgs': 
*/
number_of_msgs = msg_array.length;

/* If the text file existed and therefore was loaded "successfully", but
   no message content was found within it, the number_of_msgs will be zero.
   In this case, we don't want the user to see an empty banner, so set
   number_of_msgs to 1 and assign the default message content to the first
   array element:
*/
if (number_of_msgs == 0)
{
	msg_array[0] = default_msg;
	number_of_msgs = 1;
}

/* Loop through the array again,
   and any time that message duration is undefined, assign it
   the default message duration:
*/
for(i=0; i<number_of_msgs; i++)
{
  if (msg_duration_array[i] == null)	
  { msg_duration_array[i] = default_duration; }	
}

gotoAndStop(3);
// THIS IS FRAME 3 of 3 OF THE MAIN TIMELINE. The msg_animated movie clip exists on this frame.

stop();
// THIS IS FRAME 1 OF TIMELINE FOR MOVIE CLIP msg_animated

msg_mc.msg_txt.text = _root.msg_array[_root.current_msg];
_root.time_left = _root.msg_duration_array[_root.current_msg];
// THIS IS FRAME 'startloop' OF TIMELINE FOR MOVIE CLIP msg_animated (no code)
// THIS IS FRAME 'endloop' OF TIMELINE FOR MOVIE CLIP msg_animated

if(_root.time_left > 1)
{
	_root.time_left--;
	gotoAndPlay("startloop");	
}
// THIS IS THE 'endanimation' FRAME OF TIMELINE FOR MOVIE CLIP msg_animated

stop();

if (_root.current_msg >= (_root.number_of_msgs - 1))
{ _root.current_msg = 0; }
else 
{ _root.current_msg++; }

gotoAndPlay(1);


 
« Return


©2012 Kroll Design    info@KrollDesign.net    781.910.3694
Last modified: 12/31/1969 7:00 PM