I'm trying to spawn random bullets from the side of the screen that the player has to dodge. When I try to do this, I get this error:
Error
main.lua:69: bad argument #1 to 'remove' (table expected, got nil)
Traceback
[C]: in function 'remove'
main.lua:69: in function 'update'
[C]: in function 'xpcall'
I've tried to look around and see what I can do, but I can't find much. Sorry, if this is a newbie question, I'm REALLY new to Lua and Love2D.
Here is my entire code: (Error on line 69)
function love.load()
p = {}
p.x = 330
p.y = 330
p.score = 0
-------------
b = {}
b.size = 10
b.cooldown = 150
b.bullets = {}
b.shoot = function()
if b.cooldown <= 0 then
b.cooldown = 50
bullet = {}
bullet.x = 0
bullet.y = love.math.random(0, 690)
table.insert(b.bullets, bullet)
end
end
end
function love.update(dt)
p.score = p.score + 1
b.cooldown = b.cooldown - 1
-- movement of player (and the border)
if love.keyboard.isDown("w") then
if p.y <= 0 then
p.y = 0
end
p.y = p.y - 3
end
if love.keyboard.isDown("s") then
if p.y >= 680 then
p.y = 680
end
p.y = p.y + 3
end
if love.keyboard.isDown("a") then
if p.x <= 0 then
p.x = 0
end
p.x = p.x - 3
end
if love.keyboard.isDown("d") then
if p.x >= 680 then
p.x = 680
end
p.x = p.x + 3
end
-- cooldown wait and shoot
if b.cooldown <= 0 then
b.shoot()
end
-- removes bullet when out of sight or moves bullets forward
for i, b in ipairs(b.bullets) do
if bullet.x >= 0 then
table.remove(b.bullets, i)
end
bullet.x = bullet.x + 5
end
end
function love.draw()
love.graphics.setBackgroundColor(153, 76, 0)
love.graphics.setColor(255, 0, 0)
love.graphics.rectangle("fill", p.x, p.y, 20, 20)
love.graphics.print("p.x:" .. p.x .. " p.y:" .. p.y .. " SCORE: " .. p.score .. "Cooldown: " .. b.cooldown, 0,0,0, 1, 1)
--draw bullets
love.graphics.setColor(255,0,0)
for _,b in pairs(b.bullets) do
love.graphics.rectangle("fill", bullet.y, bullet.x, bullet.size, bullet.size)
end
end