Collision Function --- Nil?
Posted: Mon Jun 22, 2015 2:05 pm
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...
I have no idea what's going on... Please help!
Code VVV
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]
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