Can I use one listener for multiple buttons?

Hi,

Is there any way I can see what the ID is of the button pressed in the listener? If so, can I call the same listener for multiple buttons? In the listener I just want to get the ID of the button and then load another scene. The ID will be used for it somewhere else. I am placing about 20 image buttons on a scrollView control and will let the user click one of the images to select. I need to save the ID of the button so I can use it somewhere else in the app. I hate to add 20 listeners but will if I need to

Thanks!!

Warren

Yes.  In many cases(such as level buttons) that’s actually best practice.  

Thanks but how do I get the button ID in the event listener from the button that was pressed?

Hi @warrenwsav,

In the event listener (for the button), the Lua ID of the object will be returned as “event.target”, like this:

[lua]

local function buttonPress( event )

   local buttonID = event.target

end

[/lua]

Brent

Thanks but I’m not getting the ID returned. I’m getting something like “table: 07E91A50”. Here is what I have as the button code and the event listener below:

-- Function to handle button events local function handleButtonEvent( event ) local phase = event.phase if "ended" == phase then local buttonID = event.target print( buttonID ) elseif event.phase == "moved" then -- Check if you moved your finger while touching local dx = math.abs( event.x - event.xStart ) -- Get the x-transition of the touch-input local dy = math.abs( event.y - event.yStart ) -- Get the y-transition of the touch-input if dx \> 5 or dy \> 5 then scrollView:takeFocus( event ) -- If the x- or y-transition is more than 5 put the focus to your scrollview end end end local btn2 = widget.newButton{ left = 40, top = HTop, defaultFile = "men/HM2.png", overFile = "men/HM2.png", id = "HM2", onEvent = handleButtonEvent }

The print( buttonID ) is what returns the table string instead. I am wanting to get the HM2 returned. Did I do something wrong?

Thanks,

Warren

If event.target is the button, and the button.id is what you want…

(you want to use event.target.id)

That worked great - thanks!!

Warren

Yes.  In many cases(such as level buttons) that’s actually best practice.  

Thanks but how do I get the button ID in the event listener from the button that was pressed?

Hi @warrenwsav,

In the event listener (for the button), the Lua ID of the object will be returned as “event.target”, like this:

[lua]

local function buttonPress( event )

   local buttonID = event.target

end

[/lua]

Brent

Thanks but I’m not getting the ID returned. I’m getting something like “table: 07E91A50”. Here is what I have as the button code and the event listener below:

-- Function to handle button events local function handleButtonEvent( event ) local phase = event.phase if "ended" == phase then local buttonID = event.target print( buttonID ) elseif event.phase == "moved" then -- Check if you moved your finger while touching local dx = math.abs( event.x - event.xStart ) -- Get the x-transition of the touch-input local dy = math.abs( event.y - event.yStart ) -- Get the y-transition of the touch-input if dx \> 5 or dy \> 5 then scrollView:takeFocus( event ) -- If the x- or y-transition is more than 5 put the focus to your scrollview end end end local btn2 = widget.newButton{ left = 40, top = HTop, defaultFile = "men/HM2.png", overFile = "men/HM2.png", id = "HM2", onEvent = handleButtonEvent }

The print( buttonID ) is what returns the table string instead. I am wanting to get the HM2 returned. Did I do something wrong?

Thanks,

Warren

If event.target is the button, and the button.id is what you want…

(you want to use event.target.id)

That worked great - thanks!!

Warren