Collision functions not working

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
highkingtorygg
Prole
Posts: 1
Joined: Tue Oct 20, 2015 4:28 am

Collision functions not working

Post by highkingtorygg »

I was going through this
http://www.osmstudios.com/tutorials/you ... art-1-of-3
and Everything worked, up until the collision function. It says h2 is a nil value when I fire.

I also tried with the older collision function, still didn't work. If you can point out what I'm doing wrong i would help.

Code: Select all

debug = true

player =  { x = 200, y = 710, speed = 150, img = nil}

canShoot = true
canShootTimerMax = 0.2
canShootTimer = canShootTimerMax

bulletImg = nil

bullets = {}

createEnemyTimerMax = 0.4
createEnemyTimer = createEnemyTimerMax

enemyImg = nil

enemies = {}

isAlive = true
score = 0


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.load(arg)
	player.img = love.graphics.newImage('assets/plane.png')

	bulletImg = love.graphics.newImage('assets/bullet.png')

	enemyImg = love.graphics.newImage('assets/enemy.png')
end

function love.update(dt)
	--exit game
	if love.keyboard.isDown('escape') then
		love.event.push('quit')
	end

	if love.keyboard.isDown(' ', 'rctrl', 'lctrl', 'ctrl') and canShoot then
		newBullet = { x = player.x + (player.img:getWidth()/2), y = player.y, img = bulletImg }
		table.insert(bullets, newBullet)
		canShoot = false
		canShootTimer = canShootTimerMax
	end

	for i, bullet in ipairs(bullets) do
		bullet.y = bullet.y - (250 * dt)

		if bullet.y < 0 then
			table.remove(bullets, i)
		end
	end

	canShootTimer = canShootTimer - (1 * dt)
	if canShootTimer < 0 then
		canShoot = true
	end

	if love.keyboard.isDown('left','a') then
		if player.x > 0 then
			player.x = player.x - (player.speed*dt)
		end
	elseif love.keyboard.isDown('right','d') then
		if player.x < (love.graphics.getWidth() - player.img:getWidth()) then
			player.x = player.x + (player.speed*dt)
		end
	end

	createEnemyTimer = createEnemyTimer - (1 * dt)
	if createEnemyTimer < 0 then
		createEnemyTimer = createEnemyTimerMax

		randomNumber = math.random(10, love.graphics.getWidth() - 10)
		newEnemy = { x = randomNumber, y= -10, img = enemyImg }
		table.insert(enemies, newEnemy)
	end

	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.img:getWidth(), bullet.img:getHeight()) then
				table.remove(bullets, j)
				table.remove(enemies, i)
				score = score + 1
			end
		end

		if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.x, player.y, player.img:getWidth(), player.img:getHeight())
		and isAlive then
			table.remove(enemies, i)
			isAlive = false
		end
	end

	for i, enemy in ipairs(enemies) do
		enemy.y = enemy.y + (200 * dt)

		if enemy.y > 850 then
			table.remove(enemies, i)
		end
	end
end

function love.draw(dt)
	love.graphics.draw(player.img, player.x, player.y)

	for i, bullet in ipairs(bullets) do
	love.graphics.draw(bullet.img, bullet.x, bullet.y)
	end

	for i, enemy in ipairs(enemies) do
		love.graphics.draw(enemy.img, enemy.x, enemy.y)
	end
end
User avatar
ArchAngel075
Party member
Posts: 319
Joined: Mon Jun 24, 2013 5:16 am

Re: Collision functions not working

Post by ArchAngel075 »

Line 87 :

Code: Select all

//Line 87 :
CheckCollision(
      enemy.x, 
      enemy.y, 
      enemy.img:getWidth(), 
      enemy.img:getHeight(), 
      bullet.x, 
      bullet.img:getWidth(), 
      bullet.img:getHeight()
)
Missing the bullet.y

I often lay out my arguments in that form above since it is easy to count and compare.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 53 guests