Page 1 of 1

Bullet collision with enemy is causing a problem.

Posted: Thu Mar 19, 2015 11:15 am
by Jason Benham
Yeah this is my first post. And the subject might be a little unclear. The problem is that if like three or four "bullets" hit the enemy, the enemy sort of freezes in place instead of just being constantly pushed back. But this is my bullet collide with enemy code:

Code: Select all

[size=85]
function bullet_collision(dt)
	for i,v in ipairs(bullet) do
	if  enemy.x > v.x or
		enemy.y < v.y + v.height and
		enemy.y > v.y and
	    enemy.y + enemy.img:getHeight() < v.y and 
		enemy.y + enemy.img:getHeight() > v.y + v.height then
		enemy.y = -100
	    sound:play()
	end
	if  enemy.y > v.y or
		enemy.x < v.x + v.width and
	   	enemy.x > v.x and
	    enemy.x + enemy.img:getWidth() < v.x and 
	    enemy.x + enemy.img:getWidth() > v.x + v.width then
	    enemy.y = -100
	    sound:play()
	end
	if enemy.y < 0 then
		enemy.y = enemy.y + enemy.speed * dt
	end	
end
end
[/size]
And in case it's the enemy movement code. Here it is:

Code: Select all

[size=85]
function enemy_move(dt)
	if enemy.y < 0 then
		enemy.y = enemy.y + enemy.speed * dt
	end
	if	enemy.y < 720 then
		enemy.y = enemy.y + enemy.speed * dt
	end
	if enemy.y > 720 then
		enemy.y = -100
		enemy.x = love.math.random(0, 1280)
	end
end
[/size]
Thanks for the help.
Oh, here's the .love
https://www.dropbox.com/s/zhmuhwlqf41iduk/gr.love?dl=0

Re: Bullet collision with enemy is causing a problem.

Posted: Thu Mar 19, 2015 11:43 am
by Robin
That's because you don't ever remove the bullets.

Easiest way to do that is do something like:

Code: Select all

v.remove_me = true
inside the collision code and after the loop do:

Code: Select all

for i = #bullets, 1, -1 do
   if bullets[i].remove_me then
      table.remove(bullets, )
   end
end
Also: if enemy.y < 0, the enemy moves at least two times as fast, and even faster if there are multiple bullets.

Re: Bullet collision with enemy is causing a problem.

Posted: Thu Mar 19, 2015 11:51 am
by Jason Benham
Thank you for your help, if you do try the .love just know it's not the finished version and the images are just some I had on my computer that I use for testing. :megagrin: