Whats the best way to do this in Lua?

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]

The reason why your collision isn’t firing off is this line:

ob[1].t1:addEventListener("collision", ob.t1)

It should be:

ob[1].t1:addEventListener("collision", ob[1].t1)

You had the collision set to ob.t1 which doesn’t exist. It should be the same as the object you’re adding the event to (in your case ob[1].t1). [import]uid: 7849 topic_id: 7075 reply_id: 26569[/import]

no it isn’t the parent. objects don’t have parent/children.

you could set it yourself

[lua]ob[1].t1._parent = ob[1][/lua]

for each child object.

i used the _ because i think [lua]parent[/lua] is a reserved keyword for display groups. Is there a reason you’re not doing it in a display group anyway? if you did you wouldn’t have to create your own _parent variable and wouldnt have to set it on each item manually
[import]uid: 6645 topic_id: 7075 reply_id: 26606[/import]