Function not running?
Posted: Sun Apr 29, 2012 2:54 pm
So, I'm making a small little lib to make it easier to make graphics. I'm giving each button a function to run, if they are clicked.
It registers when the object is clicked fine, but it won't run the function stored in the click variable.
The function is added like so.
Example of calling it.
The above function adds a function that changes the text of the "MyLabel" object when "MyButton" is clicked (because when it's click, it should run it's clicked function).
So, when the button is clicked, it should run it's click function stored in the click variable, but it doesn't change the text when clicked, and doesn't run the function. If anyone understood, can they help?
By the way, if anyone needs so the the createButton function, here.
Code: Select all
function love.mousepressed(x, y, button)
for k, obj in pairs(Objects) do
if obj.objtype == "Button" and x > obj.xpos and x < obj.xpos + obj.width and y > obj.ypos and y < obj.ypos + obj.height and button == 1 then
obj[click]()
end
end
end
The function is added like so.
Code: Select all
function Lib.addFunc(objname, functype, func)
Objects[objname][functype] = func
end
Code: Select all
Lib.addFunc("MyButton", "click", function()
Lib.edit("MyLabel", "words", "BUTTON WORKED")
end)
So, when the button is clicked, it should run it's click function stored in the click variable, but it doesn't change the text when clicked, and doesn't run the function. If anyone understood, can they help?
By the way, if anyone needs so the the createButton function, here.
Code: Select all
Lib = {}
Objects = {}
function Lib.createButton(objname, x, y, h, w, stat, r, g, b)
Objects[objname] = {objtype = "Button", xpos = x, ypos = y, height = h, width = w, status = stat, red = r, green = g, blue = b, click = nil} --click is set to nil because it's added later with the Lib.addFunc function.
end