Hello,
This is my first game using Corona, and the problem I’m having, as you can tell from the title, is the error that keeps telling me “attempt to compare nil with number.” The code is based off of the getting started spaceship project with a bunch of modifications, but I’ve been searching for hours up hours and I’m really stuck here. Here’s the part of my code that seems to have the issue:
local function ballCleaner() for i = #ballsTable, 1, -1 do local thisBall = ballsTable[i] if (thisBall.x \< -100 or thisBall.x \> display.contentWidth + 100 or thisBall.y \< -100 or thisBall.y \> display.contentHeight + 100) then display.remove( thisBall ) table.remove( ballsTable, i ) end end end
local function onCollision( event ) if ( event.phase == "began" ) then local obj1 = event.object1 local obj2 = event.object2 if (( obj1.myName == "orange" and obj2.myName == "circle" ) or ( obj1.myName == "circle" and obj2.myName == "orange" ) ) then -- Remove both the circle and the orange display.remove( obj1 ) display.remove( obj2 ) timer.performWithDelay(10, endGame) timer.cancel(scoreTimer) for i = #ballsTable, 1, -1 do if ( ballsTable[i] == obj1 or ballsTable[i] == obj2 ) then ballsTable[i] = nil break end end end end end
The first block: basically a function that cleans up the objects that go off the screen.
The second block: when the circle and the orange collide, they both disappear, the game is ended, the score/timer stops and the game is ended.
The problem I believe stems from when the circle collides with the orange, then they both disappear and the orange is removed from “ballsTable” (sorry if the naming is a bit confusing)/changed to nil. Thus, it can’t be compared in the first block’s if then statement because it doesn’t have an .x or .y value. I’ve played around with a bunch of ideas and solutions but I think it’s time that I sought some help. Thanks so much!