help with bullets and foes

General discussion about LÖVE, Lua, game development, puns, and unicorns.
Post Reply
speedcoding101
Prole
Posts: 3
Joined: Thu Jan 23, 2014 6:07 am

help with bullets and foes

Post 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
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: help with bullets and foes

Post 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.
speedcoding101
Prole
Posts: 3
Joined: Thu Jan 23, 2014 6:07 am

Re: help with bullets and foes

Post by speedcoding101 »

Thank you so much! I'm not very good at this yet but ill get there eventually :P
User avatar
Plu
Inner party member
Posts: 722
Joined: Fri Mar 15, 2013 9:36 pm

Re: help with bullets and foes

Post by Plu »

Keep practicing :) You can only get better.
Post Reply

Who is online

Users browsing this forum: No registered users and 6 guests