Passing variables into ui button listener

Hi All,

I am unable to pass a variable into my listener function for a UI button. I’m trying to process the touch events from multiple buttons with the same listener function.

Here’s my code.

[lua]local onSitBtnTouch = function(event)
if event.phase == ‘release’ then
audio.play( tapSound )
print("event.target.pos "…event.target.pos)
end
end
local sitBtn = ui.newButton{
defaultSrc = “images/sit_button.png”,
defaultX = 109,
defaultY = 49,
overSrc = “images/sit_button_over.png”,
overX = 109,
overY = 49,
onEvent = onSitBtnTouch
}
sitBtn:setReferencePoint(display.TopLeftReferencePoint)
playerInfoGroups[curPos]:insert(sitBtn)
sitBtn.x = 0
sitBtn.y = 0
sitBtn.alpha = 1
sitBtn.pos = curPos[/lua]
Why is event.target.pos is always nil?

Thanks.
Nathan.
[import]uid: 23864 topic_id: 9213 reply_id: 309213[/import]

After posting this message I took a look at ui.lua. It turns out that the event listener for the ui button is in ui.lua. This function does not pass the event.target to your own onEvent listener. I added the following to ui.lua to pass on the event.target. It’s around line 105.

[lua]buttonEvent.phase = “release”
– Added by Nathan Tran
buttonEvent.target = event.target
– Done adding
result = onEvent( buttonEvent )[/lua] [import]uid: 23864 topic_id: 9213 reply_id: 33619[/import]