Looping changes to button text

How can I loop changes to button text like so? What is the correct syntax for ‘n’?

var n;
for (n=0; n <= project.options[0].length; n++) {
    btn_play_(n).buttonText.setText(project.options[n]);
    //  here  ^
}

Thanks

Your looping code seems to be correct, but I think the error is here:

The value index of 0 in project.options might be an array, but knowing that you also set text to project.options[n], when n could also be equal to zero, tells me that the error is definitely there.

Try this code instead:

var n;
for (n=0; n <= project.options.length; n++) {
    btn_play_(n).buttonText.setText(project.options[n]);
    //  here  ^
}

also, If "btn_play_(n)" is supposed to be a clip, change it to “btn_play_” instead.

1 Like

Many thanks, get the follwing;

var n;
for (n=0; n <= project.options.length; n++) {
    btn_play_(n).buttonText.setText(project.options[n]);
}

The error I get is : ReferenceError: btn_play_ is not defined

But the following does work;
btn_play_1.buttonText.setText(project.options[1]);
btn_play_2.buttonText.setText(project.options[2]);
btn_play_3.buttonText.setText(project.options[3]);
btn_play_4.buttonText.setText(project.options[4]);

I guess the best way is to just have it not in a loop. Thanks again.

that’s because you cannot substitute the numbers in btn_play1, btn_play_2, e.t.c. at all because they are variable names, and you can’t do things to variable names.

however, you can do this:

for (var n = 0; n < project.options.length; n++) {
  project["btn_play_" + n].buttonText.setText(project.options[n]);
}

this works because you can access clips from the Project timeline as project.clipName. and you are able to index object properties using a string by using object["property"] instead of object.property. these do the exact same thing, but you can use string concatenation to put n in the string.

if the clips you want aren’t in the project timeline, but are still in the same timeline as the script that it is running in, you can use parent["btn_play_" + n] instead. if it’s instead running a frame and the buttons are inside said frame, you can use this["btn_play_" + n];

also btn_play_(n) means “run the function btn_play_ with 1 argument which is n”

3 Likes

Many thanks to you both!

I was wondering if I could accomplish the same issue utilizing clones. Does anyone know how I could pass an id so that the cloned button works on the cloned clip;
cloning-clips.wick (67.4 KB)

you can keep a reference to the original clip in the clones by doing this:

var clone = this.clone();
clone.original = this;

then you can define a function in the original that loops through all the buttons

// neat trick: if you have an if statement and
// there is only one command in the statement
// (in the following setting this.changeButtons to a function)
// then the curly braces are not necessary
if (!this.isClone) this.changeButtons = function() {
  // a for ... of loop loops through each item in the array
  // setting the variable to the item
  for (let clone of this.clones) {
    clone.buttonText.setText(project.options[n]);
  }
};

then in the mouseclick script do this:

this.original.changeButtons();

or you could define the changeButtons function somewhere else, like in the project or frame or whatever.

3 Likes