One function - Lots of buttons

Hello Everyone! My current project has a screen with several button that all basically do the same thing. I currently have a function for each individual button. I want to minimize my code and make a single function. I don’t really know how to ask the question, so I’ll show some code to help myself. I’ll simplify it…

[lua]–THE FUNCTION
local myButtonFunction = function( event)
if event.phase == “release” then
bluecircle.x = BUTTONPRESSED.x --I want the blue circle to move to the coordinates
bluecircle.y = BUTTONPRESSED.y --of the button that was pressed
end
end

–THE BUTTON
local myButton = widget.newButton{
default = “myButtonImage.png”,
width = 50,
height = 50,
onRelease = myButtonFunction,
}[/lua]

There you go. Is this possible? If so, how is it done? Thank you!!

Nathan [import]uid: 39302 topic_id: 26233 reply_id: 326233[/import]

Yeah, there’s a code section from this link:
http://developer.anscamobile.com/forum/2011/12/03/tips-optimization-101

that does what you need:

[code]
–Table to store the buttons
local myButtons = {}

myButtons[“Pause”] = display.newImage(“pause.png”)
myButtons[“Pause”].myId = “Pause” --Set the buttons id

myButtons[“Shoot”] = display.newImage(“shoot.png”)
myButtons[“Shoot”].myId = “Shoot” --Set the buttons id

myButtons[“Move”] = display.newImage(“move.png”)
myButtons[“Move”].myId = “Move” --Set the buttons id

myButtons[“Retry”] = display.newImage(“retry.png”)
myButtons[“Retry”].myId = “Retry” --Set the buttons id

–Function to handle our buttons
local function handleButtons(event)
local target = event.target

–Handle action for each different button
if target.myId == “Pause” then
–Pause
elseif target.myId == “Shoot” then
–Shoot
elseif target.myId == “Move” then
–Move
elseif target.myId == “Retry” then
—Retry
end

return true
end

–Add event listeners for all the buttons
for i = 1, #myButtons do
myButtons[i]:addEventListener(“tap”, handleButtons)
end[/code]

You can just take the ‘handleButtons’ function and make it the listener for your existing buttons if you didn’t want to set up buttons like in the example. :slight_smile:

That link has a TON of great tips and optimizations to use. Check it out!

-Mario
[import]uid: 11636 topic_id: 26233 reply_id: 106329[/import]

Thanks! I didn’t do it exactly like the example, but it helped. I’m glad you shared that link too. :wink: [import]uid: 39302 topic_id: 26233 reply_id: 106340[/import]

No problem, glad to help!!

-Mario [import]uid: 11636 topic_id: 26233 reply_id: 106356[/import]

Sweet. I would be helpless without people like you who are willing to help! Thanks [import]uid: 39302 topic_id: 26233 reply_id: 106357[/import]

You could also do:

local myButtonFunction = function( event)  
 if event.phase == "release" then  
 bluecircle.x = event.target.x  
 bluecircle.y = event.target.y  
 end  
end  

event.target is the button you clicked [: [import]uid: 74723 topic_id: 26233 reply_id: 106360[/import]

That’s actually what I did today. :slight_smile: I got rid of 300 lines of code! out of 1,100… that seems pretty good to me. :slight_smile: [import]uid: 39302 topic_id: 26233 reply_id: 106361[/import]