[SOLVED] Change clips width in a loop

Hello :grinning:
I have multiple clips I 'd like to transform when I press a key. Like:
if (key == “p”) {
shape01.width = 51.725;
}

It works fine, but I want to do that for several clips (shape01, shape02, shape3) in a loop . If I do
for (var i = 0; i < 10 ; i++) {
shape + i +.width = 51.725;
}

I got an error. Is there a trick to do that ?
Thank you so much for your support!
Have a nice day

Here’s how I would do it:

var Shapes=[shape01,shape02,shape3]
var i;
for (i=0;i<Shapes.length;i++){
     try{
          Shapes[i].width=51.725;
     }catch(error){
          console.log("Err - Shapes["+i+"] isn't an object.");
     }
}
/// I didn't check this code, lemme know if you find any errors

Also, something I noticed:

shape+i would be equal to shape1, shape2, shape3, etc., however, you want shape01 & shape02, therefore you got an error.

Also, shape+i+.width makes no sense since your adding “.width” to the value of “shape+i” and setting the value equal to 51.725. This is more likely one of the reasons why you got the error.

Hopefully this helps, otherwise lemme know
Thanks for reading :)

1 Like

It works perfectly ,
thank you so much !

1 Like

I’d like to add where I think the problem came.

This does not work. You can’t add 2 things together like this and add a “.width” and expect it to work. Maybe there’s a way to get it to work by adding them beforehand and doing some conversions or whatever, but Hamzah already solved it a better way, so it’s fine.

shape + i is a string
you need to evaluate it as a variable
@Hamzah_Alani no need to make the array.

project.shape1=“asdasdd”;
console.log(project[“shape”+1]);

this should work (ok no 0 before 1 but still):

for (var i=1; i<howManyYouWant ; i++){
project[“shape”+i].width=51.725;
}