Here's where clicking is handled (in a for loop), and sorry for my cruddy coding and OOP.
...
for k,v in ipairs(self.Scene.View) do
...
Thanks in advance.
...
for k,v in ipairs(self.Scene.View) do
...
I have a program that tests the various functions of my API, and when I have 2 buttons, I can click neither of them unless I'm using pairs.What? Why would you need to do that?
I probably could, but most likely not.I suppose you could write your own pairs-ish function, but I seriously doubt it's worth the effort.
Why not just use pairs then? If that works, is there any reason to use ipairs? I'm afraid I'm a little confused about the question, as it's not entirely clear what your problem is or what you're trying to achieve - a little more code and context would be useful.I have a program that tests the various functions of my API, and when I have 2 buttons, I can click neither of them unless I'm using pairs.
I can't just use pairs. If I throw another button into the mix, I need to use ipairs.-snip
Why not just use pairs then? If that works, is there any reason to use ipairs? I'm afraid I'm a little confused about the question, as it's not entirely clear what your problem is or what you're trying to achieve - a little more code and context would be useful.
elseif e[1] == "mouse_click" then
--find what it clicked on
local x = e[3]
local y = e[4]
local button = e[2]
for id,t in pairs(self.Scene.View) do
if t.X <= x and t.Width + t.X >= x then --x matches
if t.Y <= y and (t.Height or 1) + t.Y >= y then --y matches, this is the right object
if t["OnClick"] ~= nil and type(t["OnClick"]) == "function" then
t:OnClick(x,y,button)
end
break
end
end
end
I look through all the objects in the scene using pairs, see if the X and Y matches up (and they do, but not in ipairs with 2 or pairs with 3), then call the OnClick function.
local me = QuickDraw.Init() --initing stuff
QuickDraw.AddObject(button) --defined earlier
me.Scene.BG = colors.black
me.Scene:Add("button", "b1")
me.Scene:Add("button", "b2")
--defining properties and stuff
…but if I through in another button, (me.Scene:Add("button","b3")), then pairs doesn't work and I have to switch to ipairs.What's the form of `self.Scene.View`? If it's a list with indexes (1..n) then there's no reason `ipairs` shouldn't work. Otherwise, you'll have to use pairs.
`ipairs` iterates through the indexes (1..#t), `pairs` through all indexes in the table, but in no defined order.
local a = {
[1]="Hello ",
[3]="World"
}
or
local a = {}
a.test = "Hi"