lua.main:40: bad argument 'ipairs' (table expected, got nil)
I have no clue why it's saying this
Code: Select all
function love.load()
bg = love.graphics.newImage("bg.png")
player = {} --new table for player
player.x = 300 -- x,y coordinates of the player
player.y = 450
player.speed = 100
enemies = {}
for i=0,7 do
enemy = {}
enemy.width = 40
enemy.height = 20
enemy.x = i * (enemy.width + 60) + 100
enemy.y = enemy.height + 100
table.insert (enemies, enemy)
end
end
function love.keyreleased(key)
if (key== " ") then
shoot()
end
end
function love.update (dt)
--keyboard for player
if love.keyboard.isDown("left") then
player.x = player.x - player.speed*dt
elseif love.keyboard.isDown("right") then
player.x = player.x + player.speed*dt
end
local remEnemy = {}
local remShot = {}
--update the shots
for i,v in ipairs(player.shots) do
--move them up up up up
v.y = v.y - dt * 100
--mark shots that are not visible for removal
if v.y < 0 then
table.insert(remShot, i)
end
--collision detection enemy
for ii,vv in ipairs(enemies) do
if CheckCollision(v.x,v.y,2,5,vv.x,vv.y,vv.width,vv.height) then
--mark that enemy for removal
table.insert(remEnemy, ii)
--remove shot
table.insert(remShot, i)
end
end
end
--remove the marked enemies
for i,v in ipairs(remEnemy) do
table.remove(enemies,v)
end
for i,v in ipairs(remShot) do
table.remove(player.shots, v)
end
--update enemies
for i,v in ipairs(enemies) do
--slow fall
v.y = v.y+dt
--check collision of ground
if v.y > 465 then
--lose
end
end
end
function love.draw()
--draw that BG
love.graphics.setColor(255,255,255,255)
love.graphics.draw(bg)
--ground
love.graphics.setColor(0,255,0,255)
love.graphics.rectangle("fill", 0, 465, 800, 150)
--player sprite
love.graphics.setColor (255,255,0,255)
love.graphics.rectangle ("fill", player.x, player.y, 30, 15)
--player shots
loge.graphics.setColor(255,255,255,255)
for i,v in ipairs(player.shots) do
love.graphics.rectangle("fill", v.x,v.y, 2, 5)
end
end
function shoot()
local shot = {}
shot.x = player.x+player.width/2
shot.y = player.y
table.insert(player.shots, shot)
end
--collision detection function
--checks if a and b overlap
--w and h mean width and height
function CheckCollision(ax1,ay1,aw,ah, bx1,by1,bw,bh)
local ax2,ay2,bx2,by2 = ax1 + aw, ay1+ ah, bx1 + bw, by1 + bh
return ax1 > bx2 and ax2 > bx1 and ay1 > by2 and ay2 > by1
end