How do i reset my level ? currently getting error's

game.lua:

It basically displays a jet and you can move it around by touching the screen. It also has a parallax background that moves to cities around

local physics = require ("physics") physics.start() local storyboard = require ("storyboard") local scene = storyboard.newScene() function scene:createScene(event) screenGroup = self.view background = display.newRect( 0, 0, display.contentWidth+100 , display.contentHeight ) background:setFillColor( 0,24,72) screenGroup:insert(background) cityFar = display.newImage("city1.png") cityFar:setReferencePoint(display.BottomLeftReferencePoint) cityFar.x =0 cityFar.y= 320 cityFar.speed =1 screenGroup:insert(cityFar) cityFarCopy = display.newImage("city1.png") cityFarCopy:setReferencePoint(display.BottomLeftReferencePoint) cityFarCopy.x =480 cityFarCopy.y= 320 cityFarCopy.speed = 1 screenGroup:insert(cityFarCopy) cityNear = display.newImage("city2.png") cityNear:setReferencePoint(display.BottomLeftReferencePoint) cityNear.x =0 cityNear.y= 320 cityNear.speed = 2 screenGroup:insert(cityNear) cityNearCopy = display.newImage("city2.png") cityNearCopy:setReferencePoint(display.BottomLeftReferencePoint) cityNearCopy.x = 480 cityNearCopy.y= 320 cityNearCopy.speed = 2 screenGroup:insert(cityNearCopy) topWall = display.newRect(0,0 , display.contentWidth, 0) physics.addBody(topWall, "static", {density = 1.0, friction = 0, bounce = 0.2, isSensor = false}) leftWall = display.newRect( 0, 0, 0, display.contentHeight ) rightWall = display.newRect( display.contentWidth , 0, 0, display.contentHeight ) physics.addBody(leftWall, "static", {density = 1.0, friction = 0, bounce = 1, isSensor = false}) physics.addBody(rightWall, "static", {density = 1.0, friction = 0, bounce = 1, isSensor = false}) physics.addBody(topWall, "static", {density = 1.0, friction = 0, bounce = 0.2, isSensor = false}) bottomWall = display.newRect( 0, display.contentHeight , display.contentWidth, 0 ) physics.addBody(bottomWall, "static", {density = 1.0, friction = 0, bounce = 0.2, isSensor = false}) jet = display.newImage("redJet.png") jet.x = 100 jet.y = 100 jet.health = 100 mine1 = display.newImage("mine.png") mine1.x = 500 mine1.y = 100 mine1.speed = math.random(2,6) mine1.initY = mine1.y mine1.amp = math.random(20,100) mine1.angle = math.random(1,360) physics.addBody(mine1, "static", {density=.1, bounce=0.1, friction=.2, radius=12}) screenGroup:insert(mine1) mine2 = display.newImage("mine.png") mine2.x = 500 mine2.y = 100 mine2.speed = math.random(2,6) mine2.initY = mine2.y mine2.amp = math.random(20,100) mine2.angle = math.random(1,360) physics.addBody(mine2, "static", {density=.1, bounce=0.1, friction=.2, radius=12}) screenGroup:insert(mine2) mine3 = display.newImage("mine.png") mine3.x = 500 mine3.y = 100 mine3.speed = math.random(2,6) mine3.initY = mine3.y mine3.amp = math.random(20,100) mine3.angle = math.random(1,360) physics.addBody(mine3, "static", {density=.1, bounce=0.1, friction=.2, radius=12}) screenGroup:insert(mine3) physics.addBody(jet, "dynamic", {density =.1 , gravity =.1, friction =.2 , raduis=12} ) end function scrollCity( self, event ) if self.x \< -475 then self.x = 480 else self.x = self.x - self.speed end end local function delay() jet.bodyType = "static" storyboard.gotoScene("resetLevel") end function onCollision( event ) if ( event.phase == "began" ) then storyboard.gotoScene("resetLevel", "fade", 400) end end function activateJets(self, event) print("force bieng applied") self:applyForce(0,-1.5,self.x,self.y) end function touchScreen( event) print("touch") if event.phase == "began" then jet.enterFrame = activateJets Runtime:addEventListener("enterFrame", jet) end if event.phase == "ended" then Runtime:removeEventListener("enterFrame", jet) end end function moveMines(self,event) if self.x \< -50 then self.x = 500 self.y = math.random(0,display.contentHeight) self.speed = math.random(2,6) self.amp = math.random(20,100) self.angle = math.random(1,360) else self.x = self.x - self.speed self.angle = self.angle + .1 self.y = self.amp\*math.sin(self.angle)+self.initY end end function scene:enterScene(event) physics.addBody(jet, "dynamic", {density =.1 , gravity =.1, friction =.2 , raduis=12} ) screenGroup:insert(jet) mine1.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine1) mine2.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine2) mine3.enterFrame = moveMines Runtime:addEventListener("enterFrame", mine3) Runtime:addEventListener( "collision", onCollision ) Runtime:addEventListener("touch",touchScreen) cityFar.enterFrame = scrollCity Runtime:addEventListener("enterFrame" , cityFar) cityNear.enterFrame = scrollCity Runtime:addEventListener("enterFrame" , cityNear) cityFarCopy.enterFrame = scrollCity Runtime:addEventListener("enterFrame" , cityFarCopy) cityNearCopy.enterFrame = scrollCity Runtime:addEventListener("enterFrame" , cityNearCopy) Runtime:addEventListener( "collision", onCollision ) end function scene:exitScene(event) Runtime:removeEventListener( "collision", onCollision ) Runtime:removeEventListener("enterFrame", mine1) Runtime:removeEventListener("enterFrame", mine2) Runtime:removeEventListener("enterFrame", mine3) Runtime:removeEventListener("enterFrame" , cityFar) Runtime:removeEventListener("enterFrame" , cityNearCopy) Runtime:removeEventListener("enterFrame" , cityNear) Runtime:removeEventListener("enterFrame" , cityFarCopy) end function scene:destroyScene(event) Runtime:removeEventListener( "collision", onCollision ) Runtime:removeEventListener("enterFrame", mine1) Runtime:removeEventListener("enterFrame", mine2) Runtime:removeEventListener("enterFrame", mine3) Runtime:removeEventListener("enterFrame" , cityFar) Runtime:removeEventListener("enterFrame" , cityNearCopy) Runtime:removeEventListener("enterFrame" , cityNear) Runtime:removeEventListener("enterFrame" , cityFarCopy) end scene:addEventListener("createScene" , scene) scene:addEventListener("enterScene" , scene) scene:addEventListener("exitScene" , scene) scene:addEventListener("destroyScene" , scene) return scene

resetLevel:

This is supposed to reset the level so you can play more than once

local storyboard = require ("storyboard") local scene = storyboard.newScene() -- background function scene:createScene(event) local screenGroup = self.view background = display.newImage("restart.png") screenGroup:insert(background) city2 = display.newImage("city2.png") city2:setReferencePoint(display.BottomLeftReferencePoint) city2.x = 0 city2.y = 320 screenGroup:insert(city2) end function start(event) if event.phase == "began" then storyboard.gotoScene("game", "fade", 400) end end function scene:enterScene(event) storyboard.purgeScene("game") Runtime:addEventListener("touch", start) end function scene:exitScene(event) Runtime:removeEventListener("touch", start) end function scene:destroyScene(event) end scene:addEventListener("createScene", scene) scene:addEventListener("enterScene", scene) scene:addEventListener("exitScene", scene) scene:addEventListener("destroyScene", scene) return scene

The error occurs when i get sent back to the game.lua screen, i get a black screen with a jet and when i click on the screen it gives me a run time error’s and the debugger crashes.

Runtime Error: ...ds\dontdie(builds)\dontdie(default)\dontdie\game.lua:130: attempt to call method 'applyForce' (a nil value) message stack traceback: ?: in function \<?:218\> [C]: in function 'applyForce' ...ds\dontdie(builds)\dontdie(default)\dontdie\game.lua:130: in function \<...ds\dontdie(builds)\dontdie(default)\dontdie\game.lua:128\> ?: in function \<?:218\> 

Can someone help me? I’m new to corona and lua

there is no need to put the remove event listeners in the destroy scene and also put the Runtime:removeEventListener(“touch”,touchScreen)

in the exit scene 

Your code is all over the place. In create scene you add “jet” and in enterscene you make it a physics body and add it to the scene group. Why not do all that in create scene. You could also load all of your functions in the main chunk and in your reset file do something like this in enter scene.

local previous\_scene\_name = storyboard.getPrevious() storyboard.removeScene(previous\_scene\_name)

As harryjnewton said you are removing your listeners twice. Just remove them in exitScene. If you have a listener atached to a object you dont need to worry about removing it storyboard will take care of it for you.

there is no need to put the remove event listeners in the destroy scene and also put the Runtime:removeEventListener(“touch”,touchScreen)

in the exit scene 

Your code is all over the place. In create scene you add “jet” and in enterscene you make it a physics body and add it to the scene group. Why not do all that in create scene. You could also load all of your functions in the main chunk and in your reset file do something like this in enter scene.

local previous\_scene\_name = storyboard.getPrevious() storyboard.removeScene(previous\_scene\_name)

As harryjnewton said you are removing your listeners twice. Just remove them in exitScene. If you have a listener atached to a object you dont need to worry about removing it storyboard will take care of it for you.