attempt to compare nil with number Corona

故事扮演 提交于 2020-01-25 07:12:17

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/17625255/attempt-to-compare-nil-with-number-corona

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!