Hey all! (especially @Andrew_Stacey)
I have been thinking about this problem, and I see no other solution, except for my ugly code. I have made a button class that automatically hooks onto the touched function, so it doesn’t need to have a myButton:touched().
button = class()
_buttons = {}
function button:init(x, y, w, h, f)
if _touched == nil then
if touched == nil then
touched = function () return end
end
_touched = touched
touched = function (touch)
for k, v in pairs(_buttons) do
v:touched(touch)
end
_touched(touch)
end
end
self.x, self.y, self.w, self.h = x, y, w, h
self.f = f or function () return end
self.touched = function (self, touch)
if math.abs(touch.x - self.x) < self.w
and math.abs(touch.y - self.y) < self.h
and touch.state == ENDED then
self.f()
end
end
self.index = #_buttons + 1
table.insert(_buttons, self.index, self)
end
function button:delete()
_buttons[self.index] = nil
end
I do have a few problems though…
- The button:delete(). I think it’s messy, and I couldn’t think of another name for it. (P.S. I was looking at the __gc() metamethod, but I couldn’t figure it out.
- The _buttons table… Eugh
- (not really a problem, just an unfinished feature) I have it so it works by rectMode(RADIUS), I want to make it so it corrects itself by each mode