Imagine I have four display objects that are always displayed together. While one of the objects is always visible, the other three are individually turned visible and not visible by a collision caused by something the user did. When all three objects become not visible, all three immediately turn visible again.
Now we take it one step further. Instead of just one set of four objects, I have twenty sets. I want to use the same event listener for all sets of this type of object.
I thought I could do something like:
(Pretend I set all the x/y stuff…)
local ob = {}
ob[1].main = display.newImage("ob.png")
ob[1].t1 = display.newImage("t.png")
ob[1].t2 = display.newImage("t.png")
ob[1].t3 = display.newImage("t.png")
ob[1].t1.collision = detectCol
ob[1].t1:addEventListener("collision", ob.t1)
-- add t2 and t3 with similar code
-- Turn off the object that's been hit, then test if all
-- objects are off. If they are, turn them all back on.
function detectCol(self, target)
local phase = event.phase
if(event.phase == "began") then
self.isVisible = false
-- Get the parent, that should be ob[?], right?
local t = self.parent
if(t.t1.isVisible == false and t.t2.isVisible == false and t.t3.isVisible == false) then
t.t1.isVisible = true
t.t2.isVisible = true
t.t3.isVisible = true
end
end
end
But that doesn’t work, obviously I don’t understand how “parent” works.
I’ve thought of some dumb ways to make it work, but I’d like some ideas for a reasonable way?
Thanks,
Sean. [import]uid: 4993 topic_id: 7075 reply_id: 307075[/import]