Attempt to compare nil with number, when i leave the scene

When i leave the game.lua scene to gameover.lua i get a Attempt to compare nil with number error

witch i understand because   function explode  deletes the ‘Jet’ but the  Runtime:removeEventListener  should prevent the error from coming up but its not, is there any fix for this?

ERROR:

Attempt to compare nil with number stack traceback: &nbsp;&nbsp;&nbsp;&nbsp;[C]: ? &nbsp;&nbsp;&nbsp;&nbsp;/Users/macuser/Desktop/spawn test/game.lua:299: in function \</Users/macuser/Desktop/spawn test/game.lua:297\> &nbsp;&nbsp;&nbsp;&nbsp;?: in function \<?:218\>

MAIN.LUA:

display.setStatusBar( display.HiddenStatusBar ) local storyboard = require "storyboard" storyboard.purgeOnSceneChange = true storyboard.gotoScene( "start" ) &nbsp;

GAME.LUA:

local storyboard = require( "storyboard" ) local scene = storyboard.newScene() \_W = display.contentWidth \_H = display.contentHeight local physics = require("physics") physics.start()&nbsp; system.setAccelerometerInterval( 50 ); -- Set Accelerometer Interval function scene:enterScene( event ) &nbsp; &nbsp; &nbsp; &nbsp; local group = self.view &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;print("game screen") -- START Function used to keep the player on screen local function wrap (event)&nbsp; ------- if jet.x \< -40 then jet.x = 415 end ---- if jet.x \> 415 then jet.x = -40 end ------- end -- END Function used to keep the player on screen &nbsp;&nbsp;&nbsp;&nbsp;Runtime:addEventListener("enterFrame", wrap) local function moveJet (event) &nbsp;&nbsp;&nbsp;&nbsp;-- Move to new Position &nbsp;&nbsp;&nbsp;&nbsp;jet.x = motionx + jet.x; &nbsp;&nbsp;&nbsp;&nbsp;jet.y = jet.y - motiony; &nbsp;&nbsp;&nbsp;&nbsp;-- Set New Angle &nbsp;&nbsp;&nbsp;&nbsp;jet.rotation = rotation; &nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;-- box&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if(jet.y\<0)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jet.y = 0; &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;if(jet.y\>510)then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;jet.y = 510; &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;-- end box end &nbsp;&nbsp;&nbsp;&nbsp;Runtime:addEventListener("enterFrame", moveJet) &nbsp; function explode() &nbsp;&nbsp;&nbsp;&nbsp;explosion.x = jet.x &nbsp;&nbsp;&nbsp;&nbsp;explosion.y = jet.y &nbsp;&nbsp;&nbsp;&nbsp;explosion.isVisible = true &nbsp;&nbsp;&nbsp;&nbsp;explosion:play() &nbsp;&nbsp;&nbsp;&nbsp;jet.isVisible = false &nbsp;&nbsp;&nbsp;&nbsp;timer.performWithDelay(1000, gameOver, 1) &nbsp; &nbsp; audio.play(explosionSound) &nbsp;&nbsp;&nbsp;&nbsp;audio.stop( helicopterSoundMusicChannel ) &nbsp;&nbsp;&nbsp;&nbsp;audio.setVolume( 0.15, { channel=explosionSound } ) -- set the volume on explosionSound end end function scene:exitScene( event ) &nbsp; &nbsp; &nbsp; &nbsp; local group = self.view Runtime:removeEventListener("enterFrame", moveJet) &nbsp; Runtime:removeEventListener("enterFrame", wrap) end

It’s hard to tell for sure because of the way your code is formatted, but if I read this right moveJet is local to enterScene.  Therefore when you try and remove it in exitScene(), moveJet is nil.

Functions that need to be seen by multiple storyboard functions should be declared above those functions as local functions.

one more little thing the players is colliding with all the different objects except for the “ceiling” and “floor” and i cant see a reason for it not ro work ? 

&nbsp;local ceiling = display.newImage("invisibleTile.png") &nbsp; &nbsp; ceiling:setReferencePoint(display.BottomLeftReferencePoint) &nbsp; &nbsp; ceiling.x = 0 &nbsp; &nbsp; ceiling.y = 20 &nbsp; &nbsp;physics.addBody( ceiling, "dynamic", { density=.3, bounce=.3, friction=.3, radius=45 } )&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;ceiling.name = "Bceiling1"&nbsp; &nbsp; &nbsp;ceiling.bodyType = "kinematic" &nbsp; &nbsp;ceiling.isSensor = true &nbsp; &nbsp;ceiling.index = #ceiling + 4 &nbsp; &nbsp;ceiling[ceiling.index] = ceiling &nbsp; &nbsp;local floor = display.newImage("invisibleTile.png") &nbsp; &nbsp; floor:setReferencePoint(display.BottomLeftReferencePoint) &nbsp; &nbsp; floor.x = 0 &nbsp; &nbsp; floor.y = 480 --510 &nbsp; &nbsp;physics.addBody( floor, "dynamic", { density=.3, bounce=.3, friction=.3, radius=45 } )&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;floor.name = "Bfloor1"&nbsp; &nbsp; &nbsp;floor.bodyType = "kinematic" &nbsp; &nbsp;floor.isSensor = true &nbsp; &nbsp;floor.index = #floor + 5 &nbsp; &nbsp;floor[floor.index] = floor local function onCollision(event) &nbsp;&nbsp;&nbsp;&nbsp;if event.phase == "began" and gameIsActive == true then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local obj1 = event.object1;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local obj2 = event.object2;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if obj1.name == "jetplayer" and obj2.name == "BCloud1" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "Bceiling1" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "Bfloor1" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "BCloud2" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score = score - 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "BCloud3" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score = score - 20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp;

It’s hard to tell for sure because of the way your code is formatted, but if I read this right moveJet is local to enterScene.  Therefore when you try and remove it in exitScene(), moveJet is nil.

Functions that need to be seen by multiple storyboard functions should be declared above those functions as local functions.

one more little thing the players is colliding with all the different objects except for the “ceiling” and “floor” and i cant see a reason for it not ro work ? 

&nbsp;local ceiling = display.newImage("invisibleTile.png") &nbsp; &nbsp; ceiling:setReferencePoint(display.BottomLeftReferencePoint) &nbsp; &nbsp; ceiling.x = 0 &nbsp; &nbsp; ceiling.y = 20 &nbsp; &nbsp;physics.addBody( ceiling, "dynamic", { density=.3, bounce=.3, friction=.3, radius=45 } )&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;ceiling.name = "Bceiling1"&nbsp; &nbsp; &nbsp;ceiling.bodyType = "kinematic" &nbsp; &nbsp;ceiling.isSensor = true &nbsp; &nbsp;ceiling.index = #ceiling + 4 &nbsp; &nbsp;ceiling[ceiling.index] = ceiling &nbsp; &nbsp;local floor = display.newImage("invisibleTile.png") &nbsp; &nbsp; floor:setReferencePoint(display.BottomLeftReferencePoint) &nbsp; &nbsp; floor.x = 0 &nbsp; &nbsp; floor.y = 480 --510 &nbsp; &nbsp;physics.addBody( floor, "dynamic", { density=.3, bounce=.3, friction=.3, radius=45 } )&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp; &nbsp; &nbsp;floor.name = "Bfloor1"&nbsp; &nbsp; &nbsp;floor.bodyType = "kinematic" &nbsp; &nbsp;floor.isSensor = true &nbsp; &nbsp;floor.index = #floor + 5 &nbsp; &nbsp;floor[floor.index] = floor local function onCollision(event) &nbsp;&nbsp;&nbsp;&nbsp;if event.phase == "began" and gameIsActive == true then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local obj1 = event.object1;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local obj2 = event.object2;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;if obj1.name == "jetplayer" and obj2.name == "BCloud1" then &nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "Bceiling1" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "Bfloor1" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;explode() &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "BCloud2" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score = score - 10 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;elseif obj1.name == "jetplayer" and obj2.name == "BCloud3" then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;score = score - 20 &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;end end &nbsp;