【发布时间】:2019-08-16 07:54:24
【问题描述】:
我正在尝试从玩家必须躲避的屏幕一侧生成随机子弹。当我尝试这样做时,我收到了这个错误:
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'
我试图环顾四周,看看我能做些什么,但找不到太多。抱歉,如果这是一个新手问题,我对 Lua 和 Love2D 真的很陌生。
这是我的完整代码:(第 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
【问题讨论】:
-
第 69 行
-
注意你是如何在你的 for 循环中覆盖
b变量的(你使用table.remove())