为什么说:"Bad argument #1 to 'remove' (table expected, got nil)'"

Why does it say: "Bad argument #1 to 'remove' (table expected, got nil)'"

我正在尝试从玩家必须躲避的屏幕一侧生成随机子弹。当我尝试执行此操作时,出现此错误:

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 行有错误。Lua 解释器抱怨你将 nil 值放入 table.remove 函数而不是预期的 table值。

让我们看看您的代码:

-- 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

您遍历 table b.bullets 并将当前值存储在变量 b 中,该变量是 for 循环的局部变量。这意味着在 for 循环内,您无法再访问包含 table bullets.

的全局 table b

由于您的本地 b 没有任何名为 bullets 的成员,b.bullets 在 for 循环中是一个 nil 值。

在 table 上使用 table.remove 你正在用 ipairs 迭代也是行不通的。假设您有这样的 table:

a = {1,2,3,4,5}

然后你这样做

for i,v in ipairs(a) do
  table.remove(a, i)
end

在使用索引 1 的第一个 运行 中,您将删除 1,这将导致新的 table {2,3,4,5}。 在使用索引 2 的下一个循环中,您将删除 3 而不是 2,因为 3 现在是您的第二个元素。删除 3 后,您的 table 为 {2,4,5}。下一轮使用索引 3,您将从 table 中删除 5。因为这是 table 中的最后一个元素,循环在 3 次迭代而不是 5 次迭代后完成,你最终会在 table 中留下 2 个元素而不是 0.

您的代码还有其他各种问题,但我建议您在继续之前先确保了解 Lua 中变量的范围。