Re: "Questions that don't deserve their own thread" thread
Posted: Fri Apr 03, 2015 3:52 am
@Naseem142 The way i used to handle buttons when i was starting out was to make a button table inside a table.
When whenever i wanted to use this data i used a for loop.
And for simplicity i made functions to handle all of this.
The same can be done for drawing and updating just add more functions to the table. I hope i helped
Code: Select all
function callback1()
print("button1")
end
function callback2(
print("button2")
end
buttons = {{ x = 10, y = 10, width = 100, height = 100, callback = callback1 }, { x = 20, y = 20, width = 25, height = 27, callback = callback2}}
Code: Select all
for i,button in ipairs(buttons) do
--do stuff with the button
end
Code: Select all
function newButton( x, y, width, height )
table.insert(buttons,{ x = x, y = y, width = width, height = height }
end
function buttonClicked( mouseX, mouseY )
for i,button in ipairs(buttons) do
if mouseX > button.x and mouseX < button.x + button.width and mouseY > button.y and mouseY < button.y + button.height then
button.callback()
end
end
end