[Resolved] Identifying Objects Touched (Generally)

I’m creating objects that have Listeners, most are buttons.
I want to attach something to each button for me to identify, upon touch.

Here’s what I’ve started, and you will see the comments have what I would love to accomplish…

local ui = require("ui")  
  
local function IdentifyTouchedObject(event)  
 print("You just touched the button with ID: ");  
end  
  
backBtn = ui.newButton{   
 default = "backButton.png",   
 over = "backButton\_over.png",   
 onRelease = IdentifyTouchedObject   
 }  
  
anotherBtn = ui.newButton{  
 default = "redButton.png",   
 over = "redButton\_over.png",   
 onRelease = IdentifyTouchedObject   
 }  
  

I used the “ui” library, since buttons are easy to create, but I do mean anything I can consider “touch-able”.

Is there a way to attach an ID or some Name to a button, or object, so when touched I can log it? [import]uid: 154122 topic_id: 28316 reply_id: 328316[/import]

Hey Marc.

You can assign each button with a .type handle like so:
[lua]backBtn.type = “back”[/lua]
And then when you touch the object backBtn you can associate the handle “back” by using “event.target.type” like so:
[lua]local function tapIt(e)
if e.target.type == “back” then
print(e.target.type) -----> prints “back” when touching the backBtn
end
end

Runtime:addEventListener(“tap”, tapIt)[/lua]

And yes you can have multiple objects with the same type name.

I hope this is what you’re looking for.

Cheers, Brian. [import]uid: 40731 topic_id: 28316 reply_id: 114395[/import]

That’s exactly what I’m looking for!
Thanks Brian [import]uid: 154122 topic_id: 28316 reply_id: 114399[/import]