# Repeated code

**URL:** <https://forum.kirupa.com/t/repeated-code/24876>\
**Category:** Uncategorized\
**Created:** [July 6, 2003, 4:58pm UTC](https://forum.kirupa.com/t/repeated-code/24876 "2003-07-06T16:58:19Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![up11](https://avatars.discourse-cdn.com/v4/letter/u/dc4da7/32.png) [@up11](https://forum.kirupa.com/u/up11)\
**Post date:** [July 6, 2003, 4:58pm UTC](https://forum.kirupa.com/t/repeated-code/24876/1 "2003-07-06T16:58:19Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 5:16pm UTC](https://forum.kirupa.com/t/repeated-code/24876/2 "2003-07-06T17:16:18Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 5:28pm UTC](https://forum.kirupa.com/t/repeated-code/24876/3 "2003-07-06T17:28:56Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 5:35pm UTC](https://forum.kirupa.com/t/repeated-code/24876/4 "2003-07-06T17:35:57Z")

</div>

Oh yeah, I forgot there was a tutorial on this 🙂

[http://www.kirupa.com/developer/mx/multiple\_dynamictext.htm](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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 5:42pm UTC](https://forum.kirupa.com/t/repeated-code/24876/5 "2003-07-06T17:42:32Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 5:43pm UTC](https://forum.kirupa.com/t/repeated-code/24876/6 "2003-07-06T17:43:44Z")

</div>

No success… ☹

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 6:09pm UTC](https://forum.kirupa.com/t/repeated-code/24876/7 "2003-07-06T18:09:19Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:24pm UTC](https://forum.kirupa.com/t/repeated-code/24876/8 "2003-07-06T19:24:57Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:28pm UTC](https://forum.kirupa.com/t/repeated-code/24876/9 "2003-07-06T19:28:19Z")

</div>

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:

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:37pm UTC](https://forum.kirupa.com/t/repeated-code/24876/10 "2003-07-06T19:37:31Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:39pm UTC](https://forum.kirupa.com/t/repeated-code/24876/11 "2003-07-06T19:39:18Z")

</div>

[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.

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:41pm UTC](https://forum.kirupa.com/t/repeated-code/24876/12 "2003-07-06T19:41:25Z")

</div>

> \*Originally posted by h88 \*  
> \*\*Works like a charm, Lost, 🙂 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 🙂 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]

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:42pm UTC](https://forum.kirupa.com/t/repeated-code/24876/13 "2003-07-06T19:42:09Z")

</div>

Thx lost and h88 for the great replies.  
🙂

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:42pm UTC](https://forum.kirupa.com/t/repeated-code/24876/14 "2003-07-06T19:42:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![system](https://canada1.discourse-cdn.com/flex011/uploads/kirupa/original/3X/6/2/621e5c11736f46532526e61be85940af4230f3e5.png) [@system](https://forum.kirupa.com/u/system)\
**Post date:** [July 6, 2003, 7:43pm UTC](https://forum.kirupa.com/t/repeated-code/24876/15 "2003-07-06T19:43:24Z")

</div>

up- : No problem.

h88: Im quick like a bunny :beam:
