hello everyone , when i go from my game.lua to gameover.lua and press restart button to go back to game.lua,afer 4-5 tries
i get a nil value runtime error and after making some changes i get an error saying
“attempt to compare nil with number”.
i dont know the exact reason of the error so can anyone please tell me why i get this error??
my game.lua file looks like this:-
local composer=require("composer") local scene=composer.newScene() local centerX=display.contentWidth\*0.5; local centerY=display.contentHeight\*0.5; local physics=require("physics") physics.start() physics.setDrawMode("hybrid") physics.setGravity(0,0); function scene:create( event ) local sceneGroup = self.view elements1 = display.newGroup() elements1.anchorChildren = true elements1.x = 0; elements1.y = 0; player=display.newImageRect("player.png",35,35); player.x=centerX; player.y=centerY; physics.addBody(player, "dynamic"); sceneGroup:insert(player); up=display.newImageRect("up.png",80,80); up.x=centerX-250; up.y=centerY+15; up.alpha=0.5; sceneGroup:insert(up); down=display.newImageRect("up.png",80,80); down.x=centerX+250; down.y=centerY+15; down.rotation=180; down.alpha=0.5; sceneGroup:insert(down); end local function upkey(event) if(event.phase=="began")then a1=transition.to(player,{time=100,y=player.y-50}); end end local function downkey(event) if(event.phase=="began")then a2=transition.to(player,{time=100,y=player.y+50}); end end local function onCollision(event) if(event.phase=="began")then composer.gotoScene("restart"); composer.removeScene("game"); end end ------------------------- function addboxes() local center1=display.newImageRect("line.png",50,50); center1.x=centerX+200; center1.y=centerY; physics.addBody(center1, "static"); elements1:insert(center1); end function move() for a = elements1.numChildren,1,-1 do if(elements1[a].x \> -100) then-- responsible for moving the columns elements1[a].x = elements1[a].x - 4 else elements1:remove(elements1[a]) end end end --------------------------- -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then composer.removeScene("start"); up:addEventListener("touch",upkey); down:addEventListener("touch",downkey); Runtime:addEventListener( "collision", onCollision ); moveColumnTimer = timer.performWithDelay(1, move, -1); addColumnTimer1 = timer.performWithDelay(2500, addboxes,1) end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then timer.cancel(moveColumnTimer); up:removeEventListener("touch",upkey); down:removeEventListener("touch",downkey); elseif ( phase == "did" ) then Runtime:removeEventListener( "collision", onCollision ); timer.cancel(moveColumnTimer) timer.cancel(addColumnTimer1) end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view elements1:removeSelf(); elements1=nil; transition.cancel(a1) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
game.lua61:attempt ‘elements1’(a nil value)
stack traceback:
game.lua61:in function’_listener’
?:in function<?:167>
?:in function<?:169>
any help please,still getting the same error…