White square collides with enemy but doesn't die

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
HedgeHog builder
Prole
Posts: 15
Joined: Sat Aug 19, 2017 9:13 am

White square collides with enemy but doesn't die

Post by HedgeHog builder »

I think this is something to do with the collisions but here is the main file:
To be more specific here are the parts which should be making the player die;

Now, the collisions I think just aren't happening, because the 'Alive' does not become false

Code: Select all

if CheckCollision(enemy.x,enemy.y,30,30,player.x,player.y,player.width,player.height) 
  and Alive then
  table.remove(enemies,i)
   Alive = false
      end
   end

if not Alive and love.keyboard.isDown('b') then
enemies = {}
bullets = {}
missiles = {}
Alive = true
Score = 0
end


function love.draw()
love.graphics.setColor(255,255,255)
if Alive then
love.graphics.rectangle('fill',player.x,player.y,player.width,player.height)
else 
love.graphics.print('press b to restart',200,0)
end
Attachments
main.lua
(4.28 KiB) Downloaded 58 times
User avatar
xNick1
Party member
Posts: 267
Joined: Wed Jun 15, 2016 8:27 am
Location: Rome, Italy

Re: White square collides with enemy but doesn't die

Post by xNick1 »

I can't look at the code right now.
Alive might be nil if you didn't set it to anything, so it wouldn't enter some conditions.
Maybe just print its value here and there?
Print(Alive)

Or maybe your square is Chuck Norris and it can't die
User avatar
Luke100000
Party member
Posts: 232
Joined: Mon Jul 22, 2013 9:17 am
Location: Austria
Contact:

Re: White square collides with enemy but doesn't die

Post by Luke100000 »

You should keep your code cleaner by using tabs, new lines, comments and stuff like that. You made an extra "end" after the first enemies loop, everything following was no longer in love.update() causing the game to crash. Also, the code which kills the player is no longer active. After fixing this error there is another problem: you wrote

Code: Select all

if CheckCollision(enemy.x,enemy.y,30,30,player.x,player.y,player.width,player.height) and Alive then
, but enemy does not exist. You need another loop

Code: Select all

	for p,v in ipairs(enemies) do
		if CheckCollision(v.x,v.y,30,30,player.x,player.y,player.width,player.height) and Alive then
			table.remove(enemies,i)
			Alive = false
		end
	end
Now it works fine, except that you can still shoot after you died. Here is the cleaned code I used to find the error:

Code: Select all

function love.load()
	player = {}
	player.x = 0
	player.y = love.graphics.getHeight()/2
	player.width = 30
	player.height = player.width
	bullets = {}
	bulletspeed = 1100
	enemies = {}
	enemySpawn = 1
	enemySpawnMax = 0.1
	Score = 0
	EnemyDebug = 0
	countdown = 10
	missiles = {}
	ScreenHeight = love.graphics.getHeight()
	ScreenWidth = love.graphics.getWidth()
	second_gun = {}
	player_health = 100
	Alive = true
end


function love.update(dt)
	if love.keyboard.isDown('a') and player.x > 0 then
		player.x = player.x - (150*dt)
	end
	if love.keyboard.isDown('d') and player.x <(love.graphics.getWidth() - player.width) then
		player.x = player.x + (150*dt)
	end
	if love.keyboard.isDown('s') and player.y < (love.graphics.getHeight() - player.height) then
		player.y = player.y + (150*dt)
	end
	if love.keyboard.isDown('w') and player.y > 0 then 
		player.y = player.y - (150*dt)
	end

	for i,v in ipairs(bullets) do
		v.x = v.x + (v.dx * dt)
		v.y = v.y + (v.dy * dt)
		if v.x < 0 or v.x > (love.graphics.getWidth() - 10) or v.y < 0 or v.y > (love.graphics.getHeight() - 10) then
			table.remove(bullets,i)
		end
		for i,v in ipairs(bullets) do
			for n,enemy in ipairs(enemies) do
				if CheckCollision(v.x,v.y,10,10, enemy.x,enemy.y,30,30) then
					table.remove(bullets,i)
					table.remove(enemies,n)
					Score = Score + 10
				end    
			end
		end
	end
	
	enemySpawn = enemySpawn - (1*dt)
	if enemySpawn < 0 then
		enemySpawn = enemySpawnMax
		randomNumber = math.random(0,love.graphics.getHeight())
		local newOpponent = {x = 800 , y = randomNumber , width = 30,height = 30}
		table.insert(enemies,newOpponent)
	end
	
	for i,enemy in ipairs(enemies) do
		enemy.x = enemy.x - (350*dt)
		if enemy.x < 0 then
			table.remove(enemies,i)
		end
	end
	
	for i,missile in ipairs(missiles) do
		missile.x = missile.x + (1200*dt)
	end
	for p,v in ipairs(enemies) do
		for i,missile in ipairs(missiles) do
			if CheckCollision(missile.x,missile.y,20,20,v.x,v.y,30,30) then
				table.remove(missiles,i)
				table.remove(enemies,p)
				Score = Score + 10
			end
		end
	end
	
	for p,v in ipairs(missiles) do
		if v.x < 0 or v.x > ScreenWidth + 20 or v.y < 0 or v.y > ScreenHeight + 20 then
			table.remove(missiles,p)
		end
	end
	
	for p,v in ipairs(enemies) do
		if CheckCollision(v.x,v.y,30,30,player.x,player.y,player.width,player.height) and Alive then
			table.remove(enemies,i)
			Alive = false
		end
	end
	
	if not Alive and love.keyboard.isDown('b') then
		enemies = {}
		bullets = {}
		missiles = {}
		Alive = true
		Score = 0
	end
end


function love.draw()
	love.graphics.setColor(255,255,255)
	
	if Alive then
		love.graphics.rectangle('fill',player.x,player.y,player.width,player.height)
	else 
		love.graphics.print('press b to restart',200,0)
	end
	
	love.graphics.print('Your score is '..Score,0,0)
	love.graphics.print(player_health,150,0)
	for i,v in ipairs(bullets) do
		love.graphics.setColor(0,0,255)
		love.graphics.rectangle('fill',v.x,v.y,10,10)
	end

	for i,v in ipairs(enemies) do
		love.graphics.setColor(1,255,100)
		love.graphics.rectangle('fill',v.x,v.y,30,30)
	end
	
	for i,missile in ipairs(missiles) do
		love.graphics.setColor(255,255,255)
		love.graphics.rectangle('fill',missile.x,missile.y,20,20)
	end
end


function love.mousepressed(x,y,scan)
	if scan == 1 then
		local bulletX = player.x + (player.width/2)
		local bulletY = player.y + (player.height/2)
		local mouseX = x
		local mouseY = y
		local angle = math.atan2((mouseY - bulletY),(mouseX - bulletX))
		local bulletDX = bulletspeed * math.cos(angle)
		local bulletDY = bulletspeed * math.sin(angle)
		table.insert(bullets,{x = bulletX , y = bulletY , dx = bulletDX , dy = bulletDY})
	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

function love.keypressed(button,x,y)
	if button == 'r' then
		local MissileSpeed = 300
		local heat = 0.4
		local maxHeat = 1
		local missileX = player.x + (player.width/3)
		local missileY = player.y + (player.height/2)
		local missileDirection = missileX + MissileSpeed
		local MissleY = MissleY
		table.insert(missiles,{x = missileX,y = missileY})
	end
end
Post Reply

Who is online

Users browsing this forum: No registered users and 221 guests