Random txt out of txt file

Hi,

I want to get a random tekst out of a txt file,
(random txt1, txt2, txt3)

txt:

&txt1=bla
&txt2=bla
&txt3=bla

Code i have so far
loadText = new loadVars();
loadText.load(“data.txt”);
loadText.html = yes;
//creating the loadVarsText function
loadText.onLoad = function() {
txt.html = true;
txt.htmlText = this.txt1;
};

If anyone knows a good turorial for this, couldn’t find anyting

Thanx

well if the rest of it is working there add this line:

randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]

just list all the “txt” variables between the [] brackets!
This creates an array of the txt variables

and replace this line:

txt.htmlText = this.txt1;

with this:

txt.htmlText = randomtxt(Math.round(Math.random()*randomtxt.length));

this chooses a random one from the array
Math.round() rounds the number between the brackets
Math.random() picks a random number! It works like this:
to pick a random number between 0 and 10 you have to put it like this:
Math.random()*10
it actually picks a random number between 0 and 1 and you have to multiply it by the a number!

hey coo, tanx…

I’m now using this code:

loadText = new loadVars();
loadText.load(“data.txt”);
loadText.html = yes;
//creating the loadVarsText function
loadText.onLoad = function() {
txt.html = true;
txt.htmlText = randomtxt(Math.round(Math.random()*randomtxt.length));
randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]
};

have a dynamic text field with the name txt

and my data.txt with:

&txt1=bla1
&txt2=bla2
&txt3=bla3
&txt4=bla4

But no text is showin gup…

any idea what i’m doing wrong

switch these lines:

txt.htmlText = randomtxt(Math.round(Math.random()*randomtxt.length));
randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]

The creation of the array should come first cuz the thing doesn’t know where to pick the random string if no array is created yet!

oeps

hmm weird still no texts is showing up:

loadText = new loadVars();
loadText.load(“data.txt”);
loadText.html = yes;
//creating the loadVarsText function
loadText.onLoad = function() {
txt.html = true;
randomtxt = [“txt1”, “txt2”, “txt3”, “txt4”]
txt.htmlText = randomtxt(Math.round(Math.random()*randomtxt.length));

};

this line:

txt.htmlText = randomtxt(Math.round(Math.random()*randomtxt.length));

should be:

txt.htmlText = this[randomtxt[Math.floor(Math.random()*randomtxt.length)]];

and…

loadText = new LoadVars();
loadText.load("data.txt");
loadText.onLoad = function() {
	var total;
	for (var vars in this) if (typeof this[vars] == "string") total++;
	txt.html=true, txt.htmlText=this["txt"+Math.ceil(Math.random()*total)];
};

that’s what i’d do… you can add as many variables as you wish in the txt file without having to edit the swf. =)

ok thanx man…it works!

no problem, leenx. :wink:

hi another question, now i want the flash file to read the txt file from top to bottom, so no random , can anyone help?

?? :slight_smile:

great!

:wink: