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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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

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.

1
2
3
4
5
buttonEvent.phase = "release"
-- Added by Nathan Tran
buttonEvent.target = event.target
-- Done adding
result = onEvent( buttonEvent )
views:850 update:2011/10/3 8:06:12
corona forums © 2003-2011