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

What is Framer? Join the Community
Return to index
Carl Cooper
Posted Aug 05 - Read on Facebook

I'm looking to create a few layers through a for loop, animate each one, and then destroy it but only one layer is being deleted. Anyone have an idea of how to do it?

Here's my code:

for num in [0..10]
layer = new Layer

animation = layer.animate
properties:
y: 1000
delay: num

animation.on "end", ->
layer.destroy()

1 Comments

Sumul Shah

Here's a working version: http://share.framerjs.com/433ry7a8sxhn/

The way you have it, every time it goes through the loop, "layer" is getting redefined to the new layer that gets created. So by the end, all of the events are trying to destroy the last layer.

To learn more about weird things happening to variables when you're setting up events in loops, read about lexical scoping and closures. It can be a bit confusing at first, but you'll get so much benefit from understanding what's going on.

Read the entire post on Facebook