Resetting values whens witching scenes?

so basically i have a game that starts with a start menu, you tap start it takes you to the game, it plays you lose all your lives and a restart scene comes up, when i click restart it goes back to the orignal game scene without resetting it i was wondering how i could reset the game scene everytime i swap to it

my points are the same from when i died and went to the restart scene, and when it swaps back to the game it keeps my score and starts spawning in twice as many objects aswell

just need to know how to reset a scene when swapping back and forth from it

I prefer using composer.removeScene() for that purpose.

https://docs.coronalabs.com/daily/api/library/composer/removeScene.html

composer.removeScene(“sceneName”)

so it kinda works for me but when ever it swaps to my endGame screen it starts giving me an error telling me that im trying to insert something into a group that doesnt exist even though it clearly exists i think its removing the group before it removes everything else 

so where about would i put the removeScene function in the scene template they provide you because it seems to give me the same exact thing everytime no matter where i put it 

try putting this at the just below function scene:create( event )

composer.recycleOnSceneChange = true

ERROR: Runtime error

E:\Game  Development\Mobile\Corona\Projects\TiltaRoll\Code\TiltaRoll\realGame.lua:280: attempt to call method ‘insert’ (a nil value)

stack traceback:

E:\Game  Development\Mobile\Corona\Projects\TiltaRoll\Code\TiltaRoll\realGame.lua:280: in function ‘_listener’

?: in function <?:167>

?: in function <?:169>

okay now im getting this error message, it only happens when i go to the restart screen my syntax is right and it works completely until i swap scenes

its giving me the same error using both options that you guys have given me, idk why its doing it though my guess is its not defining the variable that im trying to insert or the group im trying to insert to isnt being defined when i try and destroy or recycle the scene

I’m guessing you have a Runtime listener(game loop?) in the deleted scene and it’s trying to keep inserting an object into a group. Can you try looking at it before you insert the recycle code? Also, we could do much better if you could share some code (sample is also okay) with us.

heres my entire game code the only runtime listener i have is for collision, im thinking it has to do with where im inserting the objects into the group but when i try to insert it to the group in another part of the code it gives me a syntax error

here is my full code for my game minus the start and restart screen which is just some event listeners for tapping the screen

im still relatively new at this so im hoping everythings right 

and i havent added any comments in so im sorry if its confusing, im still trying to get into the habit of commenting

local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------- -- Code outside of the scene event functions below will only be executed ONCE unless -- the scene is removed entirely (not recycled) via "composer.removeScene()" -- ----------------------------------------------------------------------------------- local backgroundGroup local gameGroup local UIGroup -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- local gravity = .5 local hitPoints = 3 local timerNum = 0 local loop = 0 local hits = 0 local randomSpawn = math.random(3) local blueTime local greenTime local redTime = 1 local scoreNumber = 0 local background local platform local ball local scoreBoard local score local scoreVisual local life1 local life2 local life3 local grayDot local redDot local blueDot local greenDot local damageSquare local function endGame() composer.gotoScene("restart") end -- create() function scene:create( event ) local sceneGroup = self.view -- Code here runs when the scene is first created but has not yet appeared on screen background = display.newImageRect("background.png",320, 668 ) background.x = display.contentCenterX background.y = display.contentCenterY background:toBack() backgroundGroup = display.newGroup() backgroundGroup = display.newGroup() backgroundGroup:insert(background) sceneGroup:insert(backgroundGroup) gameGroup = display.newGroup() sceneGroup:insert( gameGroup ) UIGroup = display.newGroup() sceneGroup:insert(UIGroup) platform = display.newImageRect("Platform.png", 360, 35) platform.name = "platform" platform.x = display.contentCenterX platform.y = 505 platform.yScale = display.contentScaleY + 1 gameGroup:insert(platform) ball = display.newImageRect("square.png", 50, 50) ball.name = "ball" ball.x = display.contentCenterX ball.y = display.contentCenterY sceneGroup:insert(ball) scoreBoard = display.newImageRect("scoreboard.png", 330, 35 ) scoreBoard.x = display.contentCenterX scoreBoard.y = -30 scoreBoard:toFront() UIGroup:insert(scoreBoard) score = display.newText( "Score: ", display.contentWidth - 270, display.contentHeight - 510, native.systemFontBold, 20 ) score:setFillColor(255, 255, 255) UIGroup:insert(score) scoreVisual = display.newText( scoreNumber, 270, display.contentHeight - 510, {width = 128}, native.systemFontBold, 20, {align = "right"}) UIGroup:insert(scoreVisual) life1 = display.newImageRect("square.png", 20, 20) life1.x = 15 life1.y = 0 UIGroup:insert(life1) life2 = display.newImageRect("square.png", 20, 20) life2.x = 35 life2.y = 0 UIGroup:insert(life2) life3 = display.newImageRect("square.png", 20, 20) life3.x = 55 life3.y = 0 UIGroup:insert(life3) end -- show() function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is still off screen (but is about to come on screen) physics.start() physics.setGravity (0, 25) physics.addBody(platform, "static", {bounce = .2, friction = 1}) physics.addBody(ball, "dynamic", {bounce = 0, friction = 1}) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen local function onTouch(event) local ball = event.target local phase = event.phase if ( "began" == phase ) then -- Set touch focus on the ball display.currentStage:setFocus( ball ) -- Store initial offset position ball.touchOffsetX = event.x - ball.x elseif ( "moved" == phase ) then -- Move the ball to the new touch position ball.x = event.x - ball.touchOffsetX if ball.x \< 24 then ball.x = 24 display.currentStage:setFocus(nil) elseif event.x \> 296 then ball.x = 296 display.currentStage:setFocus(nil) end elseif ( "ended" == phase or "cancelled" == phase ) then -- Release touch focus on the ball display.currentStage:setFocus( nil ) end return true end local function onCollision(event) local phase = event.phase if phase == "began" then local obj1 = event.object1 local obj2 = event.object2 if (obj1.name == "ball" and obj2.name == "grayDot")or (obj1.name == "grayDot" and obj2.name == "ball") then display.remove(obj2) scoreNumber = scoreNumber + 250 scoreVisual.text = scoreNumber elseif (obj1.name == "platform" and obj2.name == "grayDot")or (obj1.name == "grayDot" and obj2.name == "platform") then display.remove(obj2) end if (obj1.name == "ball" and obj2.name == "redDot")or (obj1.name == "redDot" and obj2.name == "ball") then display.remove(obj2) scoreNumber = scoreNumber + 500 scoreVisual.text = scoreNumber elseif (obj1.name == "platform" and obj2.name == "redDot")or (obj1.name == "redDot" and obj2.name == "platform") then display.remove(obj2) end if (obj1.name == "ball" and obj2.name == "blueDot")or (obj1.name == "blueDot" and obj2.name == "ball") then display.remove(obj2) scoreNumber = scoreNumber + 750 scoreVisual.text = scoreNumber elseif (obj1.name == "platform" and obj2.name == "blueDot")or (obj1.name == "blueDot" and obj2.name == "platform") then display.remove(obj2) end if (obj1.name == "ball" and obj2.name == "greenDot")or (obj1.name == "greenDot" and obj2.name == "ball") then display.remove(obj2) scoreNumber = scoreNumber + 250 scoreVisual.text = scoreNumber elseif (obj1.name == "platform" and obj2.name == "greenDot")or (obj1.name == "greenDot" and obj2.name == "platform") then display.remove(obj2) end if (obj1.name == "ball" and obj2.name == "damage")or (obj1.name == "damage" and obj2.name == "ball") then display.remove(obj2) hits = hits + 1 print(hits) if hits == 1 then display.remove(life3) elseif hits == 2 then display.remove(life2) elseif hits == 3 then display.remove(life1) endGame() physics.pause() end elseif (obj1.name == "platform" and obj2.name == "damage")or (obj1.name == "damage" and obj2.name == "platform") then display.remove(obj2) end elseif phase == "ended" then end return true end function spawnRed() local function redTime() redDot = display.newImageRect("RedDot.png", 30, 30) redDot.x = math.random(20, 300) redDot.y = -60 physics.addBody(redDot, "dynamic", {bounce = 0}) redDot.gravityScale = .7 redDot.name = "redDot" gameGroup:insert(redDot) end timer.performWithDelay(5000, redTime, 0) end function damagePoint() damageSquare = display.newImageRect("whiteSquare.png", 20, 20) damageSquare.x = math.random(20, 300) damageSquare.y = -60 damageSquare.name = "damage" physics.addBody(damageSquare, "dynamic", {bounce = 0}) damageSquare.gravityScale = 1.5 gameGroup:insert(damageSquare) end function spawnBlue() local function blueTime() blueDot = display.newImageRect("blueDot.png", 30, 30) blueDot.x = math.random(20, 300) blueDot.y = -60 physics.addBody(blueDot, "dynamic", {bounce = 0}) blueDot.gravityScale = 2 blueDot.name = "blueDot" gameGroup:insert(blueDot) end timer.performWithDelay(8000, blueTime, 0) end function spawnGreen() local function greenTime() greenDot = display.newImageRect("greenDot.png", 30, 30) greenDot.x = math.random(20, 300) greenDot.y = -60 physics.addBody(greenDot, "dynamic", {bounce = 0}) greenDot.gravityScale = 3 greenDot.name = "greenDot" gameGroup:insert(greenDot) end timer.performWithDelay(15000, greenTime, 0) end function spawnGray() grayDot = display.newImageRect("gray\_dot.png", 30, 30) grayDot.x = math.random(20, 300) grayDot.y = -60 physics.addBody(grayDot, "dynamic", {bounce = 0}) grayDot.gravityScale = .5 grayDot.name = "grayDot" gameGroup:insert(grayDot) end Runtime:addEventListener("collision", onCollision) ball:addEventListener("touch", onTouch) end if loop == 0 then timer.performWithDelay(1000, spawnGray, 0) timer.performWithDelay(6000, spawnRed) timer.performWithDelay(12000, spawnBlue) timer.performWithDelay(18000, spawnGreen) timer.performWithDelay(6000, damagePoint, 0) end end -- hide() function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Code here runs when the scene is on screen (but is about to go off screen) elseif ( phase == "did" ) then -- Code here runs immediately after the scene goes entirely off screen end end -- destroy() function scene:destroy( event ) local sceneGroup = self.view -- Code here runs prior to the removal of scene's view end -- ----------------------------------------------------------------------------------- -- Scene event function listeners -- ----------------------------------------------------------------------------------- scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ----------------------------------------------------------------------------------- return scene

I might have found the problem. I’m thinking it’s with your timers. Here:

if loop == 0 then timer.performWithDelay(1000, spawnGray, 0) timer.performWithDelay(6000, spawnRed) timer.performWithDelay(12000, spawnBlue) timer.performWithDelay(18000, spawnGreen) timer.performWithDelay(6000, damagePoint, 0) end

You create timers and don’t cancel them when you are changing the scene. That’s probably why it’s trying to insert gray / red / blue / green to the display group but can’t find it because Composer already removed those objects. Can you try canceling the timers before you change scene and tell the result?

I usually create my timers to a variable and insert them into a table. When the time comes (somewhere around scene hide phase), I remove all the table elements by first canceling then making them nil. For example:

-- creating timers local tableTimers = {} local tmr = timer.performWithDelay(1000, doSomething, 1) table.insert(tableTimers, tmr) -- removing timers at scene hide -- check for table size to be sure print ("before", #tableTimers) for i = #tableTimers, 1, -1 do if (tableTimers[i]) then timer.cancel(tableTimers[i]) tableTimers[i] = nil end end print ("after", #tableTimers)

Also, don’t forget to call removeScene() on both restart screen show(“did”) phase and game screen show(“did”) phase for each other.

okay i believe i did it right i got them into the table but now its still giving me the same error

By the way, I wrote table.insert() part wrong. I just corrected it. Can you try it out and if it not works, zip your code and upload to some place so I can take a look?

yea haha i figured that out but its just giving me the same error still

heres the zip file http://www.mediafire.com/download/fmsxt8fi68l29w9/Test_Game%282%29.rar

im still new so its probably extremely enificiant but its giving me something to learn on(i have a fear of being judged)

Ok, so my bad :slight_smile: I have written the for loop wrong so I edited the post with the code and tried it in your project. You need to change your for loop to the one below and it should be fixed.

for i = #tableTimers, 1, -1 do if (tableTimers[i]) then timer.cancel(tableTimers[i]) tableTimers[i] = nil end end

it worked until i restarted it, it gave me the same error when it swaps back to the game scene

Ok, I got it. The problem is because you called all your functions from the scene:show() function which executes twice to handle “will” and “did” phases. You need to make them execute once by simply putting them into the corresponding “if” clause. Also, you should follow the template for a better practice and clean code:
https://docs.coronalabs.com/api/library/composer/index.html#template

everything works now you are the best dude seriosuly thanks for taking the time to help me 

:slight_smile: No problem, glad all is well now.

okay so after testing the game i was making i found out that the error still comes up but its only every once in and awhile(which is alot better than what it was originally), that insert error that i kept getting, its only like once every 10 to 11 games thats played, so im guessing the problem is what we thought before where its trying to insert the object into the group that doesnt exist because its being removed before its inserted, but im not to sure on how i could fix it

I prefer using composer.removeScene() for that purpose.

https://docs.coronalabs.com/daily/api/library/composer/removeScene.html

composer.removeScene(“sceneName”)

so it kinda works for me but when ever it swaps to my endGame screen it starts giving me an error telling me that im trying to insert something into a group that doesnt exist even though it clearly exists i think its removing the group before it removes everything else 

so where about would i put the removeScene function in the scene template they provide you because it seems to give me the same exact thing everytime no matter where i put it 

try putting this at the just below function scene:create( event )

composer.recycleOnSceneChange = true