local Class = require("hump.class")
local Obj = Class()
function Obj:init()
self.x = 100
end
function Obj:add(n)
self.x = self.x + n
end
return Obj
This is the only real way you can do it.
The first example you gave creates a new function for every object, which kind of defeats the point of the Class system.
Obj = class{}
function Obj:init()
self.x = 0
end
function Obj:timeFunction()
self.x = self.x + self.var
end
function Obj:add(var)
self.var = var
timer.after(1, self.timeFunction)
end
Obj = class{}
function Obj:init()
self.x = 0
self.timeFunction = function() self.x = self.x + self.var end
end
function Obj:add(var)
self.var = var
timer.after(1, self.timeFunction)
end
It's no big deal since I can make every method who don't need argument in the constructor but I can do what I want without using a library so I was wondering if I could do it with. (like this)
Your problem doesn't lie in hump.class, it's how you use timer.after. timer.after doesn't add a 'self' argument so you need to wrap/bind your function:
Obj = class{}
function Obj:init()
self.x = 0
end
function Obj:timeFunction()
self.x = self.x + self.var
end
function Obj:add(var)
self.var = var
timer.after(1, function() self:timeFunction() end)
end
Also i wouldn't recommend to store 'var' in self for the one second, because if :add() get's called a lot there will be bugs.