# How to make this code easier?

**URL:** <https://forum.kirupa.com/t/how-to-make-this-code-easier/24871>\
**Category:** Uncategorized\
**Created:** [July 6, 2003, 2:59pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871 "2003-07-06T14:59:58Z")\
**Posts on this page:** 8\
**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, 2:59pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/1 "2003-07-06T14:59:58Z")

</div>

I have several buttons to load different txt files into an textfield. This is action for button1:[AS]loadText = new loadVars();  
loadText.load(“textfile.txt”);  
loadText.onLoad = function(OK) {  
if (OK) {  
myText.html = true;  
myText.htmlText = this.text1;  
}  
};[/AS]  
The problem is that i have like 30 buttons  
Can anyone help me to set a function to make this easier?

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, 3:29pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/2 "2003-07-06T15:29:17Z")

</div>

well an easy way would be to generate the buttons dynamically (i.e duplicatemovieclip)…

u would just have to make an mc (lets call this “mcWithButton” - set the linkage in the library to this name too…) and put the button in there… take your actions and put em in the first frame of the mc withg the button… change the actions to the following…

[AS]  
loadText = new loadVars();  
loadText.load(“textfile.txt”);  
loadText.onLoad = function(OK) {  
if (OK) {  
myText.html = true;  
myText.htmlText = this[“text” + butnum];  
}  
};  
[/AS]

then make another mc (for duplicating the mcWithButton). In this mc’s first frame actions, put in the following actions.

[AS]  
curX = [x of first button];  
curY = [y of first button];  
for(i = 1; i \< 31; i++){

this.attachmovie(“mcWithButton”,“button” + i,i);  
this[“button” + i].\_x = curX;  
this[“button” + i].\_y = curY;

this.curX += [X offset to next button];  
this.curY += [Y offset to next button];  
}  
[/AS]

That should do it… :beam:

if thats not what u wanted then… we can come up with something else… :thumb:

but usually, if u need to have all the buttons made at design time, u will need to change something in each button to make em work right…

-Aditya

---

<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, 3:33pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/3 "2003-07-06T15:33:59Z")

</div>

I already have all 30 buttons on stage and was thinking maybe a function would be the solution?

---

<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, 3:43pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/4 "2003-07-06T15:43:12Z")

</div>

yeah

put the following in some frame  
[AS]  
\_global.setText = function(){

loadText = new loadVars();  
loadText.load(“textfile.txt”);  
loadText.onLoad = function(OK) {  
if (OK) {  
for(num = 1; num \< 31; num++){  
\_root[“button” + num].myText.html = true;  
\_root[“button” + num].myText.htmlText = this[“text” + num];  
}  
}  
};

}  
[/AS]

just change “\_root” to your path from where the function is to the buttons… (dont but the dot between the last part of your path and your “[” brackets… )

try that…

-Aditya

---

<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, 3:52pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/5 "2003-07-06T15:52:47Z")

</div>

Nope didnt work

---

<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, 4:33pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/6 "2003-07-06T16:33:35Z")

</div>

did u call the function from somewhere?

---

<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, 4:36pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/7 "2003-07-06T16:36:34Z")

</div>

How i do dat? Sorry im really a newbie 😛

---

<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:24pm UTC](https://forum.kirupa.com/t/how-to-make-this-code-easier/24871/8 "2003-07-06T17:24:12Z")

</div>

oki… put this line of code right after the function I gave you before…

[AS]  
\_global.setText();  
[/AS]

and thats it!

make sure all your buttons are named (Instance Name, not name in library) “button 1” , “button 2” etc…

Also change all the “\_root” in the function to the path from where the function is to where the buttons are… so if the buttons are in a movieClip called “menu” on the main scene then change “\_root” to “\_root.menu” …

hope that works

-Aditya
