I want to have one handler function for many buttons:
local btnSelected = function( event )
print("phase: " .. event.phase)
if (event.phase == "ended") then
print("button id: ")
print(event.id)
end
end
local level1Btn = ui.newButton{
default = "image.png",
over = "image.png",
id = "level1",
onRelease = btnSelected
}
The id that gets printed is this: userdata: 0xc1a3c
Instead of “level1”. Why? [import]uid: 52127 topic_id: 10199 reply_id: 310199[/import]
Because the onRelease/onPress handlers forward the touch-event, which according to the doc has the following id attribute:
event.id is a unique identifier of the chosen touch so that you can distinguish between multiple touches across events (i.e. the different events sent across multiple listener calls). The id uniquely identifies a given finger touching the screen as that touch changes state, generating new touch events.
The onEvent handler, however, manufactures its own event structure and there the event.id is a copy of your params.id.
There is also no way to get to the target-object from the event that is passed thru onRelease/onPress/onEvent, which is very inconvenient. Furthermore, the event structures are very different,
No idea what the deeper philosophical reasons are between these inconsistent event-models…
I ended-up adding an event.target attribute in the touch event-handler to all onRelease/onPress/onEvent handlers such that I can easily get to all the target, i.e. the button, attributes, like id/_id.
It seems that not being able to determine which object generated an event is quite a shortcoming in Corona.
I don’t fully understand your solution though. Can you explain what I need to do to my code? [import]uid: 52127 topic_id: 10199 reply_id: 37237[/import]
That doesn’t work, because if you use “onEvent”, the phase is always equal to “press”. But I specifically want a “release” listener, because the user can press a button but then move off of it and decide to not “release” the button. [import]uid: 52127 topic_id: 10199 reply_id: 37309[/import]
Are you sure? I copied and pasted your code, and all I get is this:
phase: press
Are there different versions of ui.lua floating around? I can never tell if I have the most updated versions of all these different libraries. Looks like I have 1.5 [import]uid: 52127 topic_id: 10199 reply_id: 37313[/import]
You’re right, it does work in a plain main.lua file. Turns out that simply inserting the button into a scrollView changes the event handler. If you do this:
local ScrollView = require("scrollView")
...
local scrollView = ScrollView.new{ top=topBoundary, bottom=bottomBoundary };
scrollView:insert(level1Btn);
it doesn’t work anymore (you get the “press” event but not the “release”. Do you happen to know the workaround for that? [import]uid: 52127 topic_id: 10199 reply_id: 37336[/import]
Yes, no matter what, even if it doesn’t scroll at all, the release event is not received. Ugh. I can’t believe I’ve spent so many hours simply trying to handle a button release… [import]uid: 52127 topic_id: 10199 reply_id: 37342[/import]