addEventListener("tap", [variable value]), is this doable?

My screens are stored into groups and I am trying to make my code as clean as possible.  For any given screen, I have newButton or other objects stored in ScreenName[ScreenName.numChildren] so I also store certain values such as ScreenName[ScreenName.numChildren].Tap = “OnTapDoThis”.  I would like to link thru my group and where I find a .Tap value I want to do ScreenName[i]:addEventListener(“tap” ScreenName[i].Tap)  I know that is not doable but is their something like an eval or something else that would allow to get this result?

Thank you

Yes, you can do a loop in your table and test if .tap is not nil, like:

for i=1, #ScreenName do if ScreenName[i].tap then ScreenName[i]:addEventListener("tap", ScreenName[i].Tap)  end end

But I don’t know if that makes sense since that would be adding all tap listener of all scenes, what I imagine should not be done.

Why don’t use composer? It is so better to separate scenes.

Thanks Renato but that’s what I had and it is not working as it is not recongnizing ScreenName[i].Tap.  BTW you forgot the comma between the “tap” and the SreenName[i].Tap.

I copied the addEventListener command from your post and didn’t notice it. Fixed it.

If it is not recognizing, it is a problem in your code, maybe are you trying to access it before it was defined? Without seeing the code it is difficult to give any more detailed help.

When I print the variable it give me the word Help, but the message given is “nil key supplied for property lookup”

The same idea is in my removeEventListener and it is easier to parse

function Help(event) print(SetupPage1[event.target.id].Tap) -- SetupPage1[event.target.id]:removeEventListener("tap", Help) SetupPage1[event.target.id]:removeEventListener("tap", SetupPage1[event.target.id].Tap) return 0 end

Yes, you can do a loop in your table and test if .tap is not nil, like:

for i=1, #ScreenName do if ScreenName[i].tap then ScreenName[i]:addEventListener("tap", ScreenName[i].Tap)  end end

But I don’t know if that makes sense since that would be adding all tap listener of all scenes, what I imagine should not be done.

Why don’t use composer? It is so better to separate scenes.

Thanks Renato but that’s what I had and it is not working as it is not recongnizing ScreenName[i].Tap.  BTW you forgot the comma between the “tap” and the SreenName[i].Tap.

I copied the addEventListener command from your post and didn’t notice it. Fixed it.

If it is not recognizing, it is a problem in your code, maybe are you trying to access it before it was defined? Without seeing the code it is difficult to give any more detailed help.

When I print the variable it give me the word Help, but the message given is “nil key supplied for property lookup”

The same idea is in my removeEventListener and it is easier to parse

function Help(event) print(SetupPage1[event.target.id].Tap) -- SetupPage1[event.target.id]:removeEventListener("tap", Help) SetupPage1[event.target.id]:removeEventListener("tap", SetupPage1[event.target.id].Tap) return 0 end