Is there a way to get the name of a layer that was clicked on?
Thanks but wouldn't that require already knowing which layer was clicked on? What if that is what I'm trying to determine? Thanks!
Maybe you can try something like this: layerA.on Events.Click, (event, layer) -> print "This layer was clicked", layer.name
From the docs: layer.on(eventName string, handler function)Start listening for an event on this layer. For a full list of events see this overview.
When an event is called the first argument is the event information. Depending on the specific event this can contain mouse positions, mouse deltas etc. The second argument is always the layer that the event occurred to.
Thanks for this...any idea how I would assign the click event to a layer that is getting created dynamically in a for loop?
No problem. For my project I did something like this: for i in [1..MAX_TILES] tileButton = window["Tile"+i] tileButton.buttonID = i
tileButton.on Events.Click, (event, layer) -> tileActiveAnim(layer.buttonID)
In the for loop I just created a var to hold to button, added a unique ID to it. Later I can know which button it is by calling 'layer.buttonID'.
Thanks Benny. Could you possibly show me how this code fits in with the new layer code? I can't seem to get the formatting right.
Could anyone possibly tell me what I'm doing wrong here? I get undefined when I print buttonID. When I try to print just instance name I get the same one every time no matter what I click. Thanks!
I believe you have to use the passed variables of the event function, so try print layer.buttonID
Hmmm...that also returns undefined. Was this what you meant? Thanks again for your help! Sorry for my lack of coding knowledge!
You have to declare your (button) id after you have created the layer, otherwise you will overwrite the variable when you created the new layer. Also not sure whether you can define the instance name like this (also needs to be after new layer). Maybe you need to do myVar.name = "instance name".