Bullet collision with enemy is causing a problem.

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
Jason Benham
Prole
Posts: 2
Joined: Thu Mar 19, 2015 11:04 am

Bullet collision with enemy is causing a problem.

Post 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
Last edited by Jason Benham on Thu Mar 19, 2015 11:49 am, edited 1 time in total.
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Bullet collision with enemy is causing a problem.

Post 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.
Help us help you: attach a .love.
Jason Benham
Prole
Posts: 2
Joined: Thu Mar 19, 2015 11:04 am

Re: Bullet collision with enemy is causing a problem.

Post 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:
Post Reply

Who is online

Users browsing this forum: Google [Bot] and 7 guests