Collision Function --- Nil?

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
Porkinator
Prole
Posts: 8
Joined: Wed Mar 25, 2015 12:58 pm

Collision Function --- Nil?

Post by Porkinator »

Hello, and good day. I have come here regarding a serious problem within my game. Everything was going smoothly, but when I finished my collision function, I opened the game, and it gave me THIS nonsense... :x

Code: Select all

Error: main.lua:103: attempt to compare nil with number
stack traceback:
	main.lua:103: in function 'CheckCollision'
	main.lua:70: in function 'update'
	[string "boot.lua"]:442: in function <[string "boot.lua"]:413>
	[C]: in function 'xpcall'
[Finished in 2.2s with exit code 1]
I have no idea what's going on... Please help!
Code VVV

Code: Select all

require = "conf"
local player
local score
local isAlive
local enemies
local createEnemyTimerMax
local createEnemyTimer
local enemyImg
local start
function love.load()
	player = {w = 30, h = 70, p_x = 100, p_y = 430, p_speed = 200, p_gravity = 1.5, yspeed = 0}
	score = 0
	start = false
	isAlive = true
	enemies = {}
	enemyImg = love.graphics.newImage('bomb.png')
	createEnemyTimerMax = 0.4
	createEnemyTimer = createEnemyTimerMax
	love.graphics.setBackgroundColor(50,100,255)
end
function love.update(dt)
	frame = dt*30
	if love.keyboard.isDown('q') then
		love.event.quit()
	elseif love.keyboard.isDown('d', 'right') then
		player.p_x = player.p_x + player.p_speed * dt
	elseif love.keyboard.isDown('a', 'left') then
		player.p_x = player.p_x - player.p_speed * dt
	elseif love.keyboard.isDown('s', 'down') then
		player.w = 70
		player.h = 30
		player.p_y = 470
	else
		player.w = 30
		player.h = 70
		player.p_y = 430
	end
	player.yspeed = player.yspeed + player.p_gravity * frame
	player.p_y = player.p_y + player.yspeed * frame
	if player.p_y > 430 then
		player.p_y = player.p_y - player.yspeed * frame
		player.yspeed = 0
	end
		if player.p_y < 0 then
			player.p_y = 0
		end
		if player.p_x < 0 then
			player.p_x = 0
		end  
		if player.p_x > love.graphics.getWidth() - player.w then
			player.p_x = love.graphics.getWidth() - player.w
		end
		if player.p_y > love.graphics.getHeight() - player.h then
			player.p_y = love.graphics.getHeight() - player.h
		end
		createEnemyTimer = createEnemyTimer - (1 * dt)
		if createEnemyTimer < 0 then
			createEnemyTimer = createEnemyTimerMax
			randomNumber = math.random(10, love.graphics.getWidth() - 10)
			newEnemy = { x = (love.graphics.getWidth() - enemyImg:getWidth()), y = randomNumber, img = enemyImg }
			table.insert(enemies, newEnemy)
		end
		for i, enemy in ipairs(enemies) do
			enemy.x = enemy.x - (200 * dt)
			if enemy.x < 0 then
				table.remove(enemies, i)
				score = score + 1;
			end
		end
		if CheckCollision(enemies.x, enemies.y, enemyImg:getWidth(), enemyImg:getHeight(), player.p_x, player.p_y, player.w, player.h)
			and isAlive then
			table.remove(enemies, i)
			isAlive = false;
	end
end
function love.draw()
	for i, enemy in ipairs(enemies) do
		love.graphics.draw(enemy.img, enemy.x, enemy.y)
	end
	love.graphics.rectangle('fill',player.p_x,player.p_y,player.w,player.h)
	love.graphics.setColor(0,0,0)
	love.graphics.print("Score: " .. score .. " | SUPER MEAT BICYCLE | 'BOOM! SQUISH! YAY!' - IGN | PLAYER X: " .. player.p_x .. " PLAYER Y: " .. player.p_y,0,0,0,1.2)
	love.graphics.setColor(100,255,20)
	love.graphics.rectangle('fill', 0, 500, 1000, 1000);
	love.graphics.setColor(255,255,20)
end
function love.keypressed(key,isrepeat)
	if key == 'up' or 'w' and player.p_y > 430 then
		player.yspeed = -20
	end
end
function love.focus(f)
	if not f then
		love.event.quit()
	else

	end
end
function love.quit()
	print('NO! My love!')
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
Why so serious, batman? :crazy:
User avatar
bartbes
Sex machine
Posts: 4946
Joined: Fri Aug 29, 2008 10:35 am
Location: The Netherlands
Contact:

Re: Collision Function --- Nil?

Post by bartbes »

Porkinator wrote:

Code: Select all

if CheckCollision(enemies.x, enemies.y, enemyImg:getWidth(), enemyImg:getHeight(), player.p_x, player.p_y, player.w, player.h)
    and isAlive then
        table.remove(enemies, i)
        isAlive = false;
end
So what is enemies.x supposed to be?
User avatar
Porkinator
Prole
Posts: 8
Joined: Wed Mar 25, 2015 12:58 pm

Re: Collision Function --- Nil?

Post by Porkinator »

bartbes wrote:
Porkinator wrote:

Code: Select all

if CheckCollision(enemies.x, enemies.y, enemyImg:getWidth(), enemyImg:getHeight(), player.p_x, player.p_y, player.w, player.h)
    and isAlive then
        table.remove(enemies, i)
        isAlive = false;
end
So what is enemies.x supposed to be?
Um, it wouldn't accept enemy.x as a parameter, so I put enemies.x, is that incorrect?
Why so serious, batman? :crazy:
User avatar
Porkinator
Prole
Posts: 8
Joined: Wed Mar 25, 2015 12:58 pm

Re: Collision Function --- Nil?

Post by Porkinator »

Code: Select all

Error: main.lua:70: attempt to index global 'enemy' (a nil value)
stack traceback:
	main.lua:70: in function 'update'
	[string "boot.lua"]:442: in function <[string "boot.lua"]:413>
	[C]: in function 'xpcall'
This is what it gives me when I change it to the 'enemy' variable. Somehow, I can use it elsewhere, but not here. What's up with that???
Why so serious, batman? :crazy:
User avatar
Robin
The Omniscient
Posts: 6506
Joined: Fri Feb 20, 2009 4:29 pm
Location: The Netherlands
Contact:

Re: Collision Function --- Nil?

Post by Robin »

The if CheckCollision ... block should be inside the for-loop just before it. That's where enemy is defined, that's where you want to check for collisions.
Help us help you: attach a .love.
User avatar
Porkinator
Prole
Posts: 8
Joined: Wed Mar 25, 2015 12:58 pm

Re: Collision Function --- Nil?

Post by Porkinator »

Robin wrote:The if CheckCollision ... block should be inside the for-loop just before it. That's where enemy is defined, that's where you want to check for collisions.
Thank you! That seems to have fixed the problem. I knew I was doing something obvious wrong...
Anyways, here's the finished result. It's a less then average arcade project I was doing, and for helping me I think you deserve to get the beta code.
MAIN.LUA

Code: Select all

require = "conf"
local player
local score
local isAlive
local enemies
local createEnemyTimerMax
local createEnemyTimer
local enemyImg
local start
local color
local ded
function love.load()
	player = {w = 30, h = 70, p_x = 100, p_y = 430, p_speed = 200, p_gravity = 1.5, yspeed = 0}
	score = 0
	start = false
	isAlive = true
	enemies = {}
	enemyImg = love.graphics.newImage('bomb.png')
	createEnemyTimerMax = 0.4
	createEnemyTimer = createEnemyTimerMax
	love.graphics.setBackgroundColor(50,100,255)
	color = { ONE = 255, TWO = 255, THREE = 20}
	ded = "ALIVE";
end
function love.update(dt)
	frame = dt*30
	if love.keyboard.isDown('q') then
		love.event.quit()
	elseif love.keyboard.isDown('d', 'right') then
		player.p_x = player.p_x + player.p_speed * dt
	elseif love.keyboard.isDown('a', 'left') then
		player.p_x = player.p_x - player.p_speed * dt
		elseif love.keyboard.isDown('r') then
			isAlive = true
			player.p_x = 100
			score = 0
	elseif love.keyboard.isDown('s', 'down') then
		player.w = 70
		player.h = 30
		player.p_y = 470
	else
		player.w = 30
		player.h = 70
		player.p_y = 430
	end
	player.yspeed = player.yspeed + player.p_gravity * frame
	player.p_y = player.p_y + player.yspeed * frame
	if player.p_y > 430 then
		player.p_y = player.p_y - player.yspeed * frame
		player.yspeed = 0
	end
		if player.p_y < 0 then
			player.p_y = 0
		end
		if player.p_x < 0 then
			player.p_x = 0
		end  
		if player.p_x > love.graphics.getWidth() - player.w then
			player.p_x = love.graphics.getWidth() - player.w
		end
		if player.p_y > love.graphics.getHeight() - player.h then
			player.p_y = love.graphics.getHeight() - player.h
		end
		createEnemyTimer = createEnemyTimer - (1 * dt)
		if createEnemyTimer < 0 then
			createEnemyTimer = createEnemyTimerMax
			randomNumber = math.random(10, love.graphics.getWidth() - 10)
			newEnemy = { x = (love.graphics.getWidth() - enemyImg:getWidth()), y = randomNumber, img = enemyImg }
			table.insert(enemies, newEnemy)
		end
		for i, enemy in ipairs(enemies) do
			enemy.x = enemy.x - (200 * dt)
			if CheckCollision(enemy.x, enemy.y, enemy.img:getWidth(), enemy.img:getHeight(), player.p_x, player.p_y, player.w, player.h)
			and isAlive then
			table.remove(enemies, i)
			isAlive = false;
			end
			if enemy.x < 0 then
				table.remove(enemies, i)
				score = score + 1;
			end
		end
end
function love.draw()
	for i, enemy in ipairs(enemies) do
		love.graphics.draw(enemy.img, enemy.x, enemy.y)
	end
	love.graphics.rectangle('fill',player.p_x,player.p_y,player.w,player.h)
	love.graphics.setColor(255,255,255)
	love.graphics.print("Score : " .. score .. " | SUPER MEAT BICYCLE | 'BOOM! SQUISH! YAY!' - IGN | PLAYER STATUS : " .. ded,0,0)
	love.graphics.print("Controls : AD/LEFT,RIGHT to move. UP/W to jump. S/DOWN to duck. R to restart. If you stop moving in mid-air, you'll teleport liek a wizard.\nIf you die you gain a horrible curse to forever have a lousy score, and you turn into a ghost. Have fun!",0,15)
	love.graphics.setColor(100,255,20)
	love.graphics.rectangle('fill', 0, 500, 1000, 1000);
	love.graphics.setColor(color.ONE,color.TWO,color.THREE)
	if isAlive == false then
		color.ONE = 50;
		color.TWO = 100;
		color.THREE = 255;
		score = -1000
		ded = 'DEAD'
	else
		ded = 'ALIVE'
		color.ONE = 255
		color.TWO = 255
		color.THREE = 20
	end

end
function love.keypressed(key,isrepeat)
	if key == 'up' or 'w' and player.p_y > 430 then
		player.yspeed = -20
	end
end
function love.focus(f)
	if not f then
		love.event.quit()
	else

	end
end
function love.quit()
	print('NO! My love!')
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
CONF.LUA

Code: Select all

-- Do not touch!
function love.conf(t)
	t.title = 'Super Meat Bicycle - The Game'
	t.version = '0.9.2'
	t.window.width = 1000
	t.window.height = 650
	t.window.resizable = false
	t.window.fullscreen = false
end
io.stdout:setvbuf('no')
Why so serious, batman? :crazy:
Post Reply

Who is online

Users browsing this forum: No registered users and 4 guests