【问题标题】:Why does it say: "Bad argument #1 to 'remove' (table expected, got nil)'"为什么它会说:“'remove' 的错误参数 #1(预期的表,得到 nil)'”
【发布时间】: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()

标签: lua love2d


【解决方案1】:

错误消息告诉您,第 69 行有错误。Lua 解释器抱怨您将 nil 值放入 table.remove 函数而不是预期的表值。

让我们看看你的代码:

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

您遍历表 b.bullets 并将当前值存储在 for 循环本地的变量 b 中。这意味着在 for 循环中,您不再能够访问包含表 bullets 的全局表 b

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

在使用 ipairs 迭代的表上使用 table.remove 也不起作用。假设您有一张这样的表:

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

你这样做

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

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

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

【讨论】:

    猜你喜欢
    • 2017-04-08
    • 1970-01-01
    • 1970-01-01
    • 2018-07-01
    • 2023-01-04
    • 2016-06-01
    • 2020-05-19
    • 1970-01-01
    • 2016-04-29
    相关资源
    最近更新 更多