简体   繁体   中英

attempt to compare nil with number Corona

I'm about to finish my game, but I ran into an error:

attempt to compare nil with number game2.lua 444

I don't have a clue on how to fix this, and I've tried quite everything, with no success. I think the function scrollSpace is causing this, because sometimes the background would scroll at an uncontrollable speed. Here is my code:

function scrollSpace(self,event)
    if self.x  < -900 then
        self.x = 800
    else 
        self.x = self.x - self.speed
    end 
end

This controls how the stuff scroll across the screen. If you guys what more code, just reply, and I'll be happy to add the requested source. One last thing: does anyone know what the following error means:

attempt to compare nil with number

this is my file code:

  coin01 = display.newImage("coin02.png")
  coin01.y = 100
  coin01.x = 500
  coin01.speed = math.random(2,6)
  coin01.initY = mine1.y
  coin01.amp   = math.random(20,100)
  coin01.angle = math.random(1,360) 

 physics.addBody(coin01, "static", {density=.1,bounce=0.1, friction=.2,radius=12})
 screenGroup:insert(coin01)
 end  -- end of create scene
**print "passed"

 function scrollSpace(self,event)
 if self.x  < -900 then
  self.x = 800
else 
self.x = self.x - self.speed
 end 
    end**
function scene:enterScene(event)

coin01.enterFrame = coins
Runtime:addEventListener ("enterFrame",coin01)  
  end 
  function scene:exitScene(event)
 ***storyboard . purgeScene ("start")
 story board . purgeScene ("restart")***
  Runtime:removeEventListener ("enterFrame",coin01)

  end

   function scene:destroyScene(event)

end

scene:addEventListener("createScene",scene)
scene:addEventListener("enterScene",scene) 
scene:addEventListener("exitScene",scene) 
scene:addEventListener("destroyScene",scene) 

return scene

Apperantly self.x is a nil value.. If you remove it somewhere and then listener is called in the same time, there wont be any self.x value.

You should post every line where you change/create self object. And also listener

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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