Repeated code

Ok, heres my problem… I have a movie clip with 30 buttons inside. Each button when pressed will load a var inside a txt file into a text field (b1 will load text1. b2 will load text2,etc…). Heres the code i have on my movie clip timeline: [AS]onClipEvent (enterFrame) {
b1.onPress = function() {
GetMethod = function () {
if (_url.substr(0, 4) == “file”) {
delete myLoadVars.ranVariable;
return “POST”;
} else {
return “GET”;
}
};
myLoadVars = myLoadVars2=new LoadVars();
myLoadVars.ranVariable = new Date().getTime();
myLoadVars2.onLoad = function(success) {
if (success) {
_root.myText.html = true;
_root.myText.htmlText = this.text1;
} else {
trace(“Error”);
}
};
myLoadVars.sendAndLoad(“textfile.txt”, myLoadVars2, GetMethod());
};[/AS]
Now here is my problem. Do I have to put that code for all 30 buttons??
There must be an easier way. Thanks in advance.

[AS]onClipEvent (enterFrame) {
b1.onPress = function() {
GetMethod = function () {
if (_url.substr(0, 4) == “file”) {
delete myLoadVars.ranVariable;
return “POST”;
} else {
return “GET”;
}
};
myLoadVars = myLoadVars2=new LoadVars();
myLoadVars.ranVariable = new Date().getTime();
myLoadVars2.onLoad = function(success) {
if (success) {
_root.myText.html = true;
_root.myText.htmlText = this.text1;
} else {
trace(“Error”);
}
};
myLoadVars.sendAndLoad(“textfile.txt”, myLoadVars2, GetMethod());
};[/AS]

Ok, time to analyze.

  1. There is no need to run this onEnterFrame, just throw it on a frame and you should be fine.

  2. There is no need to keep redefining the GetMethod function in every press.

  3. There is no need to keep recreating the LoadVars() objects in ever press

  4. There is no need to create 2 loadVars() objects.

  5. I have no clue why you are using sendAndLoad if you are just loading in text… Unless I am missing something. I will work under the assumption you are not sending any information out. And under this assumption, there is no need for the GetMethod function, so we can remove that.

  6. You don’t need to keep telling the textbox to be html true in every press.

So that leaves us with…

[AS]_root.myText.html = true;
myLoadVars = new LoadVars();
b1.onPress = function() {
myLoadVars.onLoad = function(success) {
if (success) {
_root.myText.htmlText = this.text1;
} else {
trace(“Error”);
}
};
myLoadVars.load(“textfile.txt?”+new Date().getTime());
};[/AS]

I think that should work, I can’t test it right now.

Hmm ok, but do i still have to have dat piece of code for every 30 buttons? Thx for the help.

Oh yeah, I forgot there was a tutorial on this :slight_smile:

http://www.kirupa.com/developer/mx/multiple_dynamictext.htm

And yes, you will need the onPress code for all 30 buttons although you may be able to use a for loop to optimize your code. Something like this (untested)…

[AS]_root.myText.html = true;
var numberOfButtons = 30;
var myLoadVars = new LoadVars();
for (var i = 1; i<=numberOfButtons; i++) {
this[“b”+i].varNum = i;
this[“b”+i].onPress = function() {
myLoadVars.file = “text”+this.varNum;
myLoadVars.onLoad = function(success) {
if (success) {
_root.myText.htmlText = this[this.file];
} else {
trace(“Error”);
}
};
myLoadVars.load(“textfile.txt?”+new Date().getTime());
};
}[/AS]

It assumes your buttons names are b1, b2, b3, b4, etc, etc and your text variables names are text1, text2, text3, text4, etc, etc.

Sorry if it doesn’t work, I am in a rush.

Actually I have to go now. Let me know how things work out. I will be back later.

No success… :frowning:

up: Looks working for me, though I guess you didn’t understand what are the requirements of Lost’s code. He said it already, what you exactly need is a set of TextFile variable names that looks like text1, text2, text3, text4, etc, etc. for each button that is named as b1, b2, b3, b4, etc and whenever each button is pressed, the myText textField would be replaced with the appropriate text for each button. Working now?

Thanks for checking it for me h88. And yes, all the text variables need to be in the same .txt file, seperated by an & symbol. So it would be like

text1=asdf
&text2=fffff
&text3=ggggggg
&text4=jjjjjj

Etc, etc.

Hrmm, now that I recheck you don’t need to keep reloading the file either…

[AS]
//set html to true for the textbox
_root.myText.html = true;
//set number of buttons
var numberOfButtons = 30;
//create loadVars object
var myLoadVars = new LoadVars();
//load file
myLoadVars.load(“textfile.txt?”+new Date().getTime());
//after loading display default text
myLoadVars.onLoad = function(success) {
if (success) {
_root.myText.htmlText = this.text1
} else {
trace(“Error”);
}
};
//assign actions for each button
for (var i = 1; i<=numberOfButtons; i++) {
this[“b”+i].varNum = i;
this[“b”+i].onPress = function() {
_root.myText.htmlText = myLoadVars[“text”+this.varNum];
};
}[/AS]

If anyone could test that for me :beam:

Hey lost, i think it works. Can you explain this line for me?[AS]myLoadVars.load(“news.txt?”+new Date().getTime());[/AS]
Thanks a lot

[AS]myLoadVars.load(“news.txt?”+new Date().getTime());[/AS]

It prevents file cache by attaching a unique value to the end via a query string (the ? at the end of news.txt signifies a query string) so the browser thinks it is a new unique file. Being this is a news file, you would want people to see the newest most updated text, and unless they clear their temp file they won’t, this prevents them having to do that.

*Originally posted by h88 *
**Works like a charm, Lost, :slight_smile: and BTW up-, you should test it Online since it’ve got an extra dummy random variable in the load process that avoids caching. **

Woo hoo :slight_smile: I hate not being able to test my code, especially because I tend to make a lot of little mistakes.

Remember when you taught me loadVars() h88? That was a happy day for me indeed =)

[edit]oh, you deleted your post…now i’m just talking to myself[/edit]

Thx lost and h88 for the great replies.
:slight_smile:

Oh lol lost you always spot my posts before i delete them.

up- : No problem.

h88: Im quick like a bunny :beam: