Having an issue I can't resolve.
I have a function we will call MakeButtons
MakeButtons creates some buttons using UI Library and puts them in a table we will call buttonTable that is called at the top of the app and is Global
Each UIButton calls another function doWhenPressed.
While within MakeButtons everything works as expected. buttonTable[x].isVisible works for example.
doWhenPressed takes the event.id (which is verified to have same value as x). ie buttonTable[x] =buttonTable[event.id]
However buttonTable[event.id].isVisible does not work. In fact buttonTable[x] when x is forced also does not work.
Any clues anyone?
I'm no expert in Lua, but I believe at least part of the problem is in doWhenPressed(event).
The event.id is not the same as the id you set in the button above. What you want, I think, is something like:
1 2 3 4 5 6 7 8 | function doWhenPressed(event) local t = event.target if(t.id == canToPop) then winscenario = true; else buttonTable[t.id].isVisible=false; end end |
You need to get the target of the event, which is your button, then you can look at its id.
Sean.
If I print(event.id), I have it correct index. Verified the type is number as well.
Solved: Bug maybe???
The uiButtons get called multiple times on a single button press. Maybe I am just not following the code well enough. I made a checker to ensure the button only gets pressed once and now everything is fine.
Remember, multiple events get generated each time a button is tapped.
For buttons, I always check that the event type (or "phase") is "ended", which means that the user lifted a finger from the button. But there are others, like "began" and "moved" as well. If you look at the sample code, you often see something like
1 2 3 | function dosomethingwithbutton(event) local phase = event.phase if "ended" == phase then |
so that the code is only called when the user lifts the finger from the button.
Sean.
I cleaned up the code so I could post an example
At the top of the program:
MakeButtons: