Hi guys, I've been trying to assign events to several layers, which are stored in an array (here: panels). For some reason, this loop only partially works. Only the last of the panels is affected by the correct event listeners. Any tips?
4 Comments
Marcelo Eduardo Oliveira
Hi Alexis,
The loop is adding the event to each object, but inside the event what will be called is only the last one.
Don't want to get to the technical aspects here, but you need to make sure every "step" of the loop, the event reaction is attached to *the* object in that step.
For this, if this was JS you would use the word "this" In coffeescript, you use the "@" symbol.
Whoa. Would have never guessed that one. Thanks a bunch Marcelo!
Andreas Mitschke
don't worry you can't guess this you need to know this...
Erik Edhagen
The technical explanation is that when the user touches the screen and the event callback runs, the for loop has already been run a long time ago. Therefore "panel" contains the last looped item. With the "this" keyword, you are instead accessing the actual element that triggered the event.
4 Comments