Page 1 of 1

help with bullets and foes

Posted: Thu Jan 23, 2014 6:33 am
by speedcoding101
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 ?

Code: Select all

    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

Re: help with bullets and foes

Posted: Thu Jan 23, 2014 8:15 am
by Plu
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)

Try something like this:

Code: Select all

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.

Re: help with bullets and foes

Posted: Thu Jan 23, 2014 2:27 pm
by speedcoding101
Thank you so much! I'm not very good at this yet but ill get there eventually :P

Re: help with bullets and foes

Posted: Thu Jan 23, 2014 2:54 pm
by Plu
Keep practicing :) You can only get better.