How to make this code easier?

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

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

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

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

Nope didnt work

did u call the function from somewhere?

How i do dat? Sorry im really a newbie :stuck_out_tongue:

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