This error doesn't occur every time, and I didn't find a way to trigger it every time. I'm adding my code so you could please help me solve this problem:main.lua:107: attempt to index local 'obj2' (a nil value)
Traceback
main.lua:107: in function 'checkCollision'
main.lua:75: in function 'update'
[C]: in function 'xpcall'
main:
Code: Select all
function love.load()
Object = require "classic"
require "player"
require "wall"
require "enemy"
require "gun"
require "bullet"
walls = {}
bullets = {}
player = Player()
enemies= {}
gun = Gun()
bulletTimer = 0
windowWidth = love.graphics.getWidth()
windowHeight = love.graphics.getHeight()
for i=1,10 do
table.insert(walls, Wall())
end
for i=1,3 do
table.insert(enemies, Enemy())
end
end
function love.update(dt)
bulletTimer = bulletTimer - 2 * dt
player:update(dt)
gun:update(dt)
for i=#enemies,1,-1 do
enemies[i]:update(dt)
if checkCollision(player, enemies[i]) then
player.x = 0
player.y = 0
end
end
for i=#walls,1,-1 do
if checkCollision(player, walls[i]) then
player.y = player.y - player.yspeed
player.x = player.x - player.xspeed
walls[i].color = {255, 255, 255, 255}
end
for j=#enemies,1,-1 do
if checkCollision(enemies[j], walls[i]) then
enemies[j].xspeed = - enemies[j].xspeed
end
end
for j=#bullets,1,-1 do
if checkCollision(bullets[j], walls[i]) then
table.remove(bullets, j)
end
end
end
for i=#bullets,1,-1 do
nope = 0
bullets[i]:update(dt)
if bullets[i].x > windowWidth or bullets[i].y > windowHeight or bullets[i].x < 0 or bullets[i].y < 0 then
table.remove(bullets, i)
nope = 1
end
if nope == 0 then
for j=#enemies,1,-1 do
if checkCollision(enemies[j], bullets[i]) then
table.remove(enemies, j)
table.remove(bullets, i)
end
end
end
end
end
function love.draw()
for i=1,#walls do
walls[i]:draw()
end
for i=1,#bullets do
bullets[i]:draw()
end
for i=1,#enemies do
enemies[i]:draw()
end
player:draw()
gun:draw()
end
function checkCollision(obj, obj2)
local obj_left = obj.x
local obj_right = obj.x + obj.width
local obj_top = obj.y
local obj_bottom = obj.y + obj.height
local obj2_left = obj2.x
local obj2_right = obj2.x + obj2.width
local obj2_top = obj2.y
local obj2_bottom = obj2.y + obj2.height
if obj_right > obj2_left and
obj_left < obj2_right and
obj_bottom > obj2_top and
obj_top < obj2_bottom then
return true
else
return false
end
end
function createBullet(x, y)
table.insert(bullets, Bullet())
bullets[#bullets].x = x
bullets[#bullets].y = y
if player.dir == 1 then
bullets[#bullets].yspeed = -1000
bullets[#bullets].xspeed = 0
elseif player.dir == 2 then
bullets[#bullets].yspeed = 0
bullets[#bullets].xspeed = 1000
elseif player.dir == 3 then
bullets[#bullets].yspeed = 1000
bullets[#bullets].xspeed = 0
elseif player.dir == 4 then
bullets[#bullets].yspeed = 0
bullets[#bullets].xspeed = -1000
end
end
bullet:
Code: Select all
Bullet = Object:extend()
function Bullet:new()
self.width = 10
self.height = 10
end
function Bullet:update(dt)
self.x = self.x + self.xspeed * dt
self.y = self.y + self.yspeed * dt
end
function Bullet:draw()
love.graphics.setColor(200, 200, 200, 255)
love.graphics.rectangle("fill",self.x, self.y, self.width, self.height)
end
Code: Select all
Enemy = Object:extend()
function Enemy:new()
windowWidth = love.graphics.getWidth()
windowHeight = love.graphics.getHeight()
self.x = love.math.random(100, windowWidth - 100)
self.y = love.math.random(100, windowHeight - 100)
self.width = 50
self.height = 50
self.xspeed = 50
self.killed = 0
end
function Enemy:draw()
love.graphics.setColor(255, 0, 0, 255)
love.graphics.rectangle("fill", self.x, self.y, self.width, self.height)
end
function Enemy:update(dt)
self.x = self.x + self.xspeed * dt
if self.x < 0 then
self.xspeed = 50
self.x = 0
end
if self.x + self.width > windowWidth then
self.xspeed = - 50
self.x = windowWidth - self.width
end
end