so im making this for a school project and iv got all my different tabs separated (more organized) so i have a bullet file and a rock file (rock is the meteor that you kill) and they both have tables rock = {} and bullet = {}. so to make the bullet kill the rock I'm using a CheckCollision function. it works fine with one table and one object but i can't make it work with two tables... what am i doing wrong ?
for i,v in ipairs(bullet) do
xx = v.x
yy = v.y
end
function rock.bullet(dt)
for i,v in ipairs(rock) do
if CheckCollision(v.a, -120, rock.width, rock.height, xx, xx, bullet.width, bullet.height ) then
table.remove(rock, i)
end
end
end
/code]
plz help
You have to nest the two loops in each other. Go over each item in the bullet list, and then compare each specific bullet to each item in the rock list. Right now you run over the bullets, doing nothing with them, and then run over the rocks matching them with the last bullet in the table (but only because the xx variable is a global that still exists outside the loop)
for i, b in ipairs( bullet ) do
for j, r in ipairs( rock ) do
if CheckCollision( b.x, b.y, b.width, b.height, r.x, r.y, r.width, r.height )
-- resolve collision here
end
end
end
You'll need to adapt it to your own structure of course But this is what the structure should look like.