简体   繁体   English

尝试在Lua中将nil和Number Error进行比较(Corona实验室)

[英]Attempt to compare nil with number Error in Lua (Corona Lab)

I'm completely new to the Corona (Lua). 我是Corona(Lua)的新手。 After running the game, the game seems to work perfectly, until a few seconds later when I get the following error: 'Attempt to compare nil with number' 运行游戏后,游戏似乎运行良好,直到几秒钟后出现以下错误:“尝试将nil与数字进行比较”

local function gameLoop() 局部函数gameLoop()

-- create new asteroids
createAsteroid()

-- remove asteroids which have been drifted off the screen
for i = #asteroidsTable, 1, -1 do
    local thisAsteroid = asteroidsTable [i]

    if (thisAsteroid.x < -100 or
        thisAsteroid.x > display.contentWidth  + 100 or
        thisAsteroid.y < -100 or
        thisAsteroid.y > display.contentHeight + 100 )

    then 

        display.remove( thisAsteroid )
        table.remove( asteroidsTable)

    end

end

end 结束


As you can see above, 'thisAsteroid' is in the 'asteroidsTable = {}' which is defined as a variable in top of the module and OUTSIDE of any function. 如上所示,“ thisAsteroid”位于“ asteroidsTable = {}”中,它被定义为模块顶部的变量,且位于任何函数的外部。

local asteroidsTable = { } 局部小行星表= {}

Thanks for your help! 谢谢你的帮助!

Either thisAsteroid.x , thisAsteroid.y , display.contentWidth or display.contentHeight is nil . thisAsteroid.xthisAsteroid.ydisplay.contentWidthdisplay.contentHeightnil

use print(thisAsteroid.x) etc to find out which one is nil . 使用print(thisAsteroid.x)等找出哪个是nil

You should also get a line number with with the error message that helps you find the problem. 您还应该获得带有错误消息的行号,以帮助您发现问题。

Once you have found the nil value you either have to prevent it from becoming nil or if you can't do that you should restrict your comparison to non- nil values. 一旦找到nil值,就必须防止它变为nil或者如果不能这样做,则应将比较范围限制为非nil值。

Try 尝试

-- create new asteroids
createAsteroid()

-- remove asteroids which have been drifted off the screen
for i = #asteroidsTable, 1, -1 do
    local asteroid = asteroidsTable [i]

    if (asteroid.x < -100 or
        asteroid.x > display.contentWidth  + 100 or
        asteroid.y < -100 or
        asteroid.y > display.contentHeight + 100 )

    then 
        local asteroidToRemove = table.remove(asteroidsTable, i)
        if asteroidToRemove ~= nil then
            display.remove(asteroidToRemove)
            asteroidToRemove= nil
        end
    end
end
end

From lua.org documentation 来自lua.org 文档

table.remove (list [, pos]) table.remove(列表[,pos])

Removes from list the element at position pos, returning the value of the removed element. 从列表中删除位置pos上的元素,返回已删除元素的值。 When pos is an integer between 1 and #list, it shifts down the elements list[pos+1], list[pos+2], ···, list[#list] and erases element list[#list]; 当pos是1到#list之间的整数时,它将下移元素list [pos + 1],list [pos + 2],...,list [#list]并擦除元素list [#list]; The index pos can also be 0 when #list is 0, or #list + 1; 当#list为0或#list + 1时,索引pos也可以为0。 in those cases, the function erases the element list[pos]. 在这种情况下,该函数将删除元素列表[pos]。

The default value for pos is #list, so that a call table.remove(l) removes the last element of list l pos的默认值为#list,因此调用表。remove(l)删除列表l的最后一个元素。

So with instruction table.remove(asteroidsTable) you remove last element from table asteroidsTable but you should remove the i-th element. 因此,使用指令table.remove(asteroidsTable)可以从表asteroidsTable删除最后一个元素,但是应该删除第i个元素。

Read more about removing elements from table from Corona forum . 在Corona 论坛上了解有关从表中删除元素的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM