Non-Physics collision with more objects

Hello everyone,

I need help on this non physics collision event. I’ve made a table of 3 objects(line1, line2 and line3) and object that I have named collOb. The problem is collision event works only with line1. I’ve worked with non physics collision event before but only between 2 objects not more like now. Thanks!

objects = {} for i=1, 1 do objects[i] = line , line2 ,line3 ; end local function hasKollided(obj1, obj2) if obj1 == nil then return false end if obj2 == nil then return false end local left = obj1.contentBounds.xMin \<= obj2.contentBounds.xMin and obj1.contentBounds.xMax \>= obj2.contentBounds.xMin local right = obj1.contentBounds.xMin \>= obj2.contentBounds.xMin and obj1.contentBounds.xMin \<= obj2.contentBounds.xMax local up = obj1.contentBounds.yMin \<= obj2.contentBounds.yMin and obj1.contentBounds.yMax \>= obj2.contentBounds.yMin local down = obj1.contentBounds.yMin \>= obj2.contentBounds.yMin and obj1.contentBounds.yMin \<= obj2.contentBounds.yMax return (left or right) and (up or down) end local isCheckingCollisions = false local function testKollisions() if isCheckingCollisions then return true end isCheckingCollisions = true for i=1, #objects do if hasKollided(objects[i], collOb) then number = math.random( 5 ) print(number) --- collision code here end isCheckingCollisions = false return true end

This code:

for i=1, 1 do objects[i] = line , line2 ,line3 ; en

loops once. There is only one reference in your table, not three. Are you actually defining these lines with code like:

display.newLine(x1,y1,x2,y2)

No I’m defining them like display.newImage . I still don’t know how to insert to table all of them

 

line = display.newImage("line.png") line.x = 50 line.y = 440 screenGroup:insert(line) line.myName = "line" physics.addBody(line,"dynamic",{radius=27}) line2 = display.newImage("line.png") line2.x = 160 line2.y = -10 screenGroup:insert(line2) line2.myName = "line" physics.addBody(line2,"dynamic",{radius=27}) line3 = display.newImage("line.png") line3.x = 270 line3.y = -10 screenGroup:insert(line3) line3.myName = "line" physics.addBody(line3,"dynamic",{radius=27})

Never mind, I got it. I’ve changed code to this

objects = {line, line2, line3 } for i=1, 1 do objects[i] = line, line2, line3

There are better ways to handle display objects, especially those you have in tables or groups. You should read these:

http://roaminggamer.com/2014/06/26/tracking-objects-in-tables-best-practices-corona-sdk/

http://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

I would recommend at the very least the following:

local objects = {} -- set up a table to hold some objects for i = 1, 3 do -- loop three times objects[i] = display.newImageRect("line.png") -- create three table references end

This code:

for i=1, 1 do objects[i] = line , line2 ,line3 ; en

loops once. There is only one reference in your table, not three. Are you actually defining these lines with code like:

display.newLine(x1,y1,x2,y2)

No I’m defining them like display.newImage . I still don’t know how to insert to table all of them

 

line = display.newImage("line.png") line.x = 50 line.y = 440 screenGroup:insert(line) line.myName = "line" physics.addBody(line,"dynamic",{radius=27}) line2 = display.newImage("line.png") line2.x = 160 line2.y = -10 screenGroup:insert(line2) line2.myName = "line" physics.addBody(line2,"dynamic",{radius=27}) line3 = display.newImage("line.png") line3.x = 270 line3.y = -10 screenGroup:insert(line3) line3.myName = "line" physics.addBody(line3,"dynamic",{radius=27})

Never mind, I got it. I’ve changed code to this

objects = {line, line2, line3 } for i=1, 1 do objects[i] = line, line2, line3

There are better ways to handle display objects, especially those you have in tables or groups. You should read these:

http://roaminggamer.com/2014/06/26/tracking-objects-in-tables-best-practices-corona-sdk/

http://coronalabs.com/blog/2011/09/14/how-to-spawn-objects-the-right-way/

I would recommend at the very least the following:

local objects = {} -- set up a table to hold some objects for i = 1, 3 do -- loop three times objects[i] = display.newImageRect("line.png") -- create three table references end