External txt

I have a text file called “names.txt”. It has several variables called name1, name2… etc.

In my fla I have a text box called firstname where names should appear.

This is the code:


names = new loadVars();
names.load("names.txt");
button.onPress = function() {
	a++;
	firstname.text = names.eval("name"+a);
};

Nothing happens…

Any idea?

Thanks.

It’s most likely because you haven’t declared a. You’re increasing a with one, but a doesn’t exist. Therefore it doesn’t work.

Another thing to remember is that the data might not be loaded yet when that button is being pressed. It is safer to only set the onPress handler when the file has loaded. This is done using onLoad:


a=0;
names = new loadVars();
names.load("names.txt");
names.onLoad = function(){
button.onPress = function() {
        a++;
        firstname.text = names["name"+a];
}
}