Collision issue with 'w2'

Questions about the LÖVE API, installing LÖVE and other support related questions go here.
Forum rules
Before you make a thread asking for help, read this.
Post Reply
User avatar
StormtrooperCat
Prole
Posts: 11
Joined: Fri Mar 09, 2018 12:29 am
Contact:

Collision issue with 'w2'

Post by StormtrooperCat »

I am very new to Löve and am trying to make a basic top-down shooter game, but I am having an issue with collision.
When I try to fire it comes up with an error:
My error code
My error code
Capture.PNG (10.35 KiB) Viewed 2085 times
When i add this code the error occurs. I am using bounding box.lua

Code: Select all

function update
for i, enemy in ipairs(enemies) do
	for j, bullet in ipairs(bullets) do
		if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), bullet.x, bullet.y, bullet.width, bullet.height) then
			table.remove(bullets, j)
			table.remove(enemies, i)
			kills = kills + 1
		end
	end
end

function CheckCollision(x1,y1,w1,h1, x2,y2,w2,h2)
  return x1 < x2+w2 and
         x2 < x1+w1 and
         y1 < y2+h2 and
         y2 < y1+h1
end
What really confuses me is i am also using this code to check my player-enemy collision and it works.

Thanks
grump
Party member
Posts: 947
Joined: Sat Jul 22, 2017 7:43 pm

Re: Collision issue with 'w2'

Post by grump »

bullet.width is nil, meaning no value has been assigned. Check the code where you're setting the width of the bullets.

Possibly unrelated to your error: you're removing items from tables while iterating over them, that results in items being skipped over:

Code: Select all

local items = { 'a', 'b', 'c', 'd', 'e' }
for k, v in ipairs(items) do
	print(k, v)
	table.remove(items, k)
end
This code will print

Code: Select all

1	a
2	c
3	e
An easy solution to that problem: iterate backwards, from end to start:

Code: Select all

local items = { 'a', 'b', 'c', 'd', 'e' }
for k = #items, 1, -1 do
	print(k, items[k])
	table.remove(items, k)
end

Code: Select all

5	e
4	d
3	c
2	b
1	a
User avatar
StormtrooperCat
Prole
Posts: 11
Joined: Fri Mar 09, 2018 12:29 am
Contact:

Re: Collision issue with 'w2'

Post by StormtrooperCat »

Nevermind, i fixed it. I was trying to collide a circle to a rectangle using bounding box, which doesn't work.
Post Reply

Who is online

Users browsing this forum: No registered users and 44 guests