Hey, I started making making a game with Love2d, and when creating a function for bullet collision, I came across the following error:
enemy.lua:106: attempt to perform arithmetic on field 'width' (a nil value)
Line 106 in enemy.lua is the following:
Code: Select all
if va.x + va.width > v.x and
Code: Select all
enemy = {}
enemy.timer = 0
enemy.timerLim = math.random(3,5)
enemy.amount = math.random(1,3)
enemy.side = math.random(1,4)
function enemy.generate(dt)
enemy.timer = enemy.timer + dt
if enemy.timer > enemy.timerLim then
-- SPAWN ENEMY --
for i=1,enemy.amount do
if enemy.side == 1 then -- LEFT
enemy.spawn(-50,screenHeight / 2 - 25)
end
if enemy.side == 2 then -- TOP
enemy.spawn(screenHeight / 2 - 25, -50)
end
if enemy.side == 2 then -- RIGHT
enemy.spawn(screenWidth,screenHeight / 2 - 25)
end
if enemy.side == 2 then -- BOTTOM
enemy.spawn(screenWidth / 2 - 25,screenHeight)
end
enemy.side = math.random(1,4)
end
enemy.amount = math.random(1,3)
enemy.timerLim = math.random(3,5)
enemy.timer = 0
end
end
enemy.width = 50
enemy.height = 50
enemy.speed = 1000
enemy.friction = 10
enemyImg = love.graphics.newImage("enemy.png")
function enemy.spawn(x,y)
table.insert(enemy, {x = x, y = y,xvel = 0,yvel = 0,health = 2,width = enemy.width,height = enemy.height})
end
function enemy.draw()
for i,v in ipairs(enemy) do
love.graphics.setColor(255,255,255)
love.graphics.draw(enemyImg,v.x,v.y)
end
end
function enemy.physics(dt)
for i,v in ipairs(enemy) do
v.x = v.x + v.xvel * dt
v.y = v.y + v.yvel * dt
v.xvel = v.xvel * (1 - math.min(dt*enemy.friction, 1))
v.yvel = v.yvel * (1 - math.min(dt*enemy.friction, 1))
end
end
function enemy.AI(dt)
for i,v in ipairs(enemy) do
-- X AXIS --
if player.x + player.width < v.x + v.width then
if v.xvel > -enemy.speed then
v.xvel = v.xvel - enemy.speed * dt
end
end
if player.x + player.width > v.x + v.width then
if v.xvel < enemy.speed then
v.xvel = v.xvel + enemy.speed * dt
end
end
-- Y AXIS --
if player.y + player.height < v.y + v.height then
if v.yvel > -enemy.speed then
v.yvel = v.yvel - enemy.speed * dt
end
end
if player.y + player.height > v.y + v.height then
if v.yvel < enemy.speed then
v.yvel = v.yvel + enemy.speed * dt
end
end
end
end
function enemy.bullet_collide()
for i,v in ipairs(enemy) do
for ia,va in ipairs(bullet) do
if va.x + va.width > v.x and
va.x < v.x + v.width and
va.y + va.height > v.y and
va.y < v.y + v.height then
table.remove(enemy, i)
table.remove(bullet, ia)
end
end
end
end
-- PARENT FUNCTIONS --
function DRAW_ENEMY()
enemy.draw()
end
function UPDATE_ENEMY(dt)
enemy.physics(dt)
enemy.AI(dt)
enemy.generate(dt)
enemy.bullet_collide()
end
Code: Select all
bullet = {}
bullet.width = 5
bullet.height = 5
bullet.speed = 500
function bullet.spawn(x,y,dir)
table.insert(bullet, {x = x, y = y, dir = dir, width = bullet.width, height = bullet.height})
end
function bullet.draw()
for i,v in ipairs(bullet) do
love.graphics.setColor(0,0,0)
love.graphics.rectangle('fill',v.x,v.y,bullet.width,bullet.height)
love.graphics.rectangle('line',v.x,v.y,bullet.width,bullet.height)
end
end
function bullet.move(dt)
for i,v in ipairs(bullet) do
if v.dir == 'up' then
v.y = v.y - bullet.speed * dt
end
if v.dir == 'down' then
v.y = v.y + bullet.speed * dt
end
if v.dir == 'right' then
v.x = v.x + bullet.speed * dt
end
if v.dir == 'left' then
v.x = v.x - bullet.speed * dt
end
end
end
-- PARENT FUNCTIONS --
function DRAW_BULLET()
bullet.draw()
end
function UPDATE_BULLET(dt)
bullet.move(dt)
end