This is a read-only archive of the Framer Community on Facebook.

What is Framer? Join the Community
Return to index
Jorge Alvarez
Posted Aug 06 - Read on Facebook

Here's an issue I come across often. I have to create a bunch of layers with the same properties, than I will then need to recall individually. I'll do a for to create all of them, and then do something like:

box.name= "box_"+ (i+1)

For some reason, I'm unable to then call those items using their name, as in:

box_1.opacity = 0

Any clues on why this doesn't work and how to do it properly?
Thanks!

3 Comments

Gregory J. Orton

Jorge Alvarez - hello! hope London is good!

instead of creating loads of loose variables, standard practice is to create a collection in an array or object, then you don't need to worry about getting "box_1.opacity" and can do just "boxes[0]" or whatever.

boxes = []

for i in [0..9]
boxes.push(new Layer)

print(boxes)

However, if you want to name them nicely, and keep them in a collection for tidiness...

boxes = []

for i in [0..9]
box = new Layer
box.name = "box_" + i
boxes[i] = box

print(boxes)

then to do ALL the boxes in a loop:

x = 0
for j in [0..9]
x = x + 50
boxes[j].x = x

for example would smear all the boxes across the x-axis

Jorge Alvarez

An array was definitely my next thing to try. Thanks Gregory! :D

Brent Riddell

When you create the layers in a loop they aren't available outside of the loop scope, to make them available use: window["box_#{i}"] = new Layer ... Then later you can simply refer to them as you want: box_1.opacity, etc ... If you want the name to show up in the layer list then also set their name property when you create them as well "box_#{i}" :)

Read the entire post on Facebook