What is the best way to reload/restart the current scene with composer and keep information?

Would I have to remove all existing eventListeners manually?

All runtime event need to be removed via Runtime:removeEventListener(“the event here”, theFunctionHere)

Ideally you would put this in scene:destroy but you may want to also put in scene:hide and I would put all Runtime:addEventListener in scene:show did phase. You may have forward declare like so
local iAmAFunction

local function iAmFuntion2()
iAmAFunction= function()
print(“we called I am a function”)
end
end

iAmFuntion2()
iAmAFunction()

Scene template here
https://docs.coronalabs.com/api/library/composer/index.html#template

So, remove allEventListeners in the scene:hide and in the scene:destroy, and forward declare all functions?

I apologize that I have so many questions.

Thank you.

You might have to foward declare depending on how your code looks, but I would try to but you level1 lua inside the composer template which I linked to above in my previous post. Thier is no magic api that removes all event listeners(that I am aware unless you code one yourself but never mind) you have to remove them manually in scene: destroy and hide. If you don’t remove them you could have a memory leak or bad performance. Because you are calling add event listeners to Runtime every time you call composer.gotoscene(“level1”) and basically adding a pound to app event time you use event listeners if you don’t remove the weight properly the app will start to slow down.

I have tried everything and it still does not work, I keep getting all kinds of errors. Here is my level1.lua and my level1-Try-Again.lua, 

 ----------------------------------------------------------------------------------------- -- -- level1.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "physics" library local physics = require "physics" physics.start() physics.setGravity(0, 18) -------------------------------------------- -- forward declarations and other locals local screenW, screenH, halfW = display.actualContentWidth, display.actualContentHeight, display.contentCenterX local vx, vy local lives = {} -- table that will hold the lives object local livesCount = 4 -- the number of lives the player has local canJump = false local horizon local horizon2 local mountainback local mountainback2 local mountain local mountain2 local trees local trees2 local ground local beetleSheetData = {width=166.67, height=106, numFrames=10, sheetContentWidth=500, sheetContentHeight=424} local scorpionSheetData = {width=169, height=108, numFrames=10, sheetContentWidth=1690, sheetContentHeight=108} local vultureSheetData = {width=121, height=117.25, numFrames=4, sheetContentWidth=121, sheetContentHeight=469} local beetle = graphics.newImageSheet("beetle-walking2.png", beetleSheetData) local scorpion = graphics.newImageSheet("scorpion-walking.png", scorpionSheetData) local vulture = graphics.newImageSheet("vulture-flying.png", vultureSheetData) local scorpionSequenceData = { {name="moving", start=1, count=10, time=1000, loopCount=0} } local beetleSequenceData = { {name="moving", start=1, count=10, time=1000, loopCount=0} } local vultureSequenceData = { {name="flapping", start=1, count=4, time=500, loopCount=0} } local enemy = {} local timeCounter = 0 -- how much time has passed in the game local enemyCounter = 0 -- number of enemies sent local enemySendSpeed = 90 -- how often to send the enemies local enemyTravelSpeed = 7000 -- how fast enemies travel across the screen local enemyIncrementSpeed = 1 -- how much to increase the enemy speed local enemyMaxSendSpeed = 55 -- max send speed, if this is not set, the enemies could just be one big flood local physicsData = (require "enemies").physicsData(1.0) function scene:create( event ) -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. local sceneGroup = self.view horizon = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/horizon(5).png", 1425, 950) horizon.x = \_CX horizon.y = \_CY horizon.value = 0.2 horizon2 = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/horizon(5).png", 1425, 950) horizon2.x = horizon.width \* 1.4 horizon2.y = \_CY horizon2.value = 0.2 mountainback = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/mountainback(4).png", 1425, 950) mountainback.x = \_CX mountainback.y = \_CY mountainback.value = 0.4 mountainback2 = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/mountainback(4).png", 1425, 950) mountainback2.x = mountainback.width \* 1.4 mountainback2.y = \_CY mountainback2.value = 0.4 mountain = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/mountain(3).png", 1425, 950) mountain.x = \_CX mountain.y = \_CY mountain.value = 1 mountain2 = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/mountain(3).png", 1425, 950) mountain2.x = mountain.width \* 1.4 mountain2.y = \_CY mountain2.value = 1 trees = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/trees(2).png", 1425, 950) trees.x = trees.width \* 1.4 trees.y = \_CY trees.value = 5 trees2 = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/trees(2).png", 1425, 950) trees2.x = trees.width \* 1.4 trees2.y = \_CY trees2.value = 5 ground = display.newImageRect("Sprite Sheets and Projects/Mountains/PALALLAX/ground(1).png", 1425, 950) ground.x = \_CX ground.y = \_CY ground.id = "ground" local groundShape = { -halfW \* 1.4, ground.height \* 0.32, halfW \* 1.4, ground.height \* 0.32, halfW \* 1.4, ground.height \* 0.52, -halfW \* 1.4, ground.height \* 0.52} physics.addBody( ground, "static", { friction=1, bounce = 0, shape=groundShape }) for i=1, livesCount do lives[i] = display.newImageRect("heart.png", 50, 50) lives[i].x = \_L + (i \* 65) - 25 lives[i].y = \_T + 50 end function scrollMountains(self,event) if self.x \< -self.width \* 0.6 then self.x = self.width \* 1.32 else self.x = self.x - self.value end end horizon.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", horizon) horizon2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", horizon2) mountainback.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountainback) mountainback2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountainback2) mountain.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountain) mountain2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountain2) trees.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", trees) trees2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", trees2) local characterOptions = { width = 100, height = 126.5, numFrames = 10, sheetContentWidth = 500, sheetContentHeight = 253 } local sheet\_character = graphics.newImageSheet("girl-running.png", characterOptions) local sequence\_character = { {name = "run", start = 1, count = 10, time = 800, loopCount = 0, loopDirection = "forward"} } local character = display.newSprite(sheet\_character, sequence\_character) character.x = \_L + 250 character.y = \_CY - (character.height \* 0.3) character:play() character.id = "character" physics.addBody(character, "dynamic", {bounce = 0}) character.isFixedRotation = true function touchScreen(event) if (event.phase == "began") then if (canJump == true) then character:applyForce(0, -45, self.x, self.y) end canJump = false end return true end local function sendEnemies() timeCounter = timeCounter + 1 if((timeCounter%enemySendSpeed) == 0) then enemyCounter = enemyCounter + 1 enemySendSpeed = enemySendSpeed - enemyIncrementSpeed if(enemySendSpeed \<= enemyMaxSendSpeed) then enemySendSpeed = enemyMaxSendSpeed end local temp = math.random(1,3) if(temp == 1) then enemy[enemyCounter] = display.newSprite(beetle, beetleSequenceData) elseif (temp == 2) then enemy[enemyCounter] = display.newSprite(scorpion, scorpionSequenceData) else enemy[enemyCounter] = display.newSprite(vulture, vultureSequenceData) end enemy[enemyCounter].x = \_R + 150 enemy[enemyCounter].y = \_CY - (\_CH \* 0.1) enemy[enemyCounter].xScale = -1 enemy[enemyCounter].id = "enemy" enemy[enemyCounter].isFixedRotation = true transition.to(enemy[enemyCounter], {x=\_L - 50, time=enemyTravelSpeed, onComplete= function(self) if (self~= nil) then display.remove(self); end end}) if(temp == 1) then physics.addBody(enemy[enemyCounter], "dynamic", physicsData:get("beetle")) elseif(temp == 2) then physics.addBody(enemy[enemyCounter], "dynamic", physicsData:get("scorpion")) elseif(temp == 3) then physics.addBody(enemy[enemyCounter], "dynamic", physicsData:get("vulture")) enemy[enemyCounter].gravityScale = 0 end enemy[enemyCounter]:setSequence("moving") enemy[enemyCounter]:play() end end function onGameOver() Runtime:removeEventListener("touch", touchScreen) Runtime:removeEventListener("enterFrame", sendEnemies) Runtime:removeEventListener("collision", onCollision) transition.pause() for i=1,#enemy do if(enemy[i] ~= nil) then display.remove(enemy[i]) enemy[i] = nil end end composer.gotoScene("level1-Try-Again", "fade", 500) end local function resetLevel() character.x = \_L + 250 character.y = \_CY - (character.height \* 0.3) for i = 1, #enemy do display.remove(enemy[i]) enemy[i] = nil end end physics.setDrawMode("hybrid") local function playerHit() character.x = \_L + 250 character.alpha = 1 lives[livesCount].alpha = 0 livesCount = livesCount - 1 if(livesCount \< 1) then onGameOver() end end local function onCollision(event) local function removeOnPlayerHit(obj1, obj2) if(obj1 ~= nil and obj1.id == "enemy") then display.remove(obj1) end if(obj2 ~= nil and obj2.id == "enemy") then display.remove(obj2) end end local function showPlayerHit() character.alpha = 0.5 local tmr\_onPlayerHit = timer.performWithDelay(1, playerHit, 1) end if(event.object1.id == "enemy" and event.object2.id == "character") then showPlayerHit() removeOnPlayerHit(event.object1, nil) end if(event.object1.id == "character" and event.object2.id == "enemy") then showPlayerHit() removeOnPlayerHit(nil, event.object2) end if(event.object1.id == "enemy" and event.object2.id == "enemy") then event.contact.isEnabled = false end if(event.object1.id == "character" and event.object2.id == "ground") then canJump = true end if(event.object1.id == "ground" and event.object2.id == "character") then canJump = true end end Runtime:addEventListener("enterFrame", sendEnemies) Runtime:addEventListener("touch", touchScreen) Runtime:addEventListener("collision", onCollision) sceneGroup:insert(horizon) sceneGroup:insert(horizon2) sceneGroup:insert(mountainback) sceneGroup:insert(mountainback2) sceneGroup:insert(mountain) sceneGroup:insert(mountain2) sceneGroup:insert(trees) sceneGroup:insert(trees2) sceneGroup:insert(ground) sceneGroup:insert(character) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene physics.pause() Runtime:removeEventListener("enterFrame", sendEnemies) elseif phase == "did" then end end function scene:destroy( event ) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene----------------------------------------------------------------------------------------- level1-Try-Again.lua(Not actually in the document): local composer = require( "composer" ) local scene = composer.newScene() local widget = require "widget" widget.setTheme("widget\_theme\_ios7") -- ----------------------------------------------------------------------------------- -- 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 tryAgain -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view function tryAgain() composer.removeScene("level1") composer.gotoScene("level1", "fade", 500) end -- Code here runs when the scene is first created but has not yet appeared on screen local background = display.newImageRect("black.png", 1500, 938) background.x = \_CX background.y = \_CY tryAgain = widget.newButton( { id = "tryAgain", label = "Try again", onEvent = tryAgain } ) tryAgain.x = \_CX tryAgain.y = \_CY sceneGroup:insert(background) sceneGroup:insert(tryAgain) 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) elseif ( phase == "did" ) then -- Code here runs when the scene is entirely on screen 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&nbsp;

What do I have to do in both documents to ensure that this will work? The composer is something very new to me, and I have been stuck on this problem for quite sometime. Can you look through my code and make any changes that are necessary, thank you very much. 

Sincerely,

Alex

What error are you getting

I was trying to put the eventListeners where you said, and I got an eventListener cannot be nil error.

You need to foward declared(outside the scene:create) onColllison, touchScreen, and send enmities to put them inside scene destroy, hide, and show

So, addEventListeners in phase == did for show and hide, and remove them in destroy. Is this correct?

Would the same thing apply to the eventListeners for my background scrolling: 

local function scrollMountains(self,event) if self.x \< -self.width \* 0.6 then self.x = self.width \* 1.32 else self.x = self.x - self.value end end horizon.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", horizon) horizon2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", horizon2) mountainback.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountainback) mountainback2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountainback2) mountain.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountain) mountain2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", mountain2) trees.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", trees) trees2.enterFrame = scrollMountains Runtime:addEventListener("enterFrame", trees2)

Again, feel free to make changes to the documents I posted and send them back. Thank you.

Sincerely,

Alex

Only do add event listeners for collision, touch, and enter frame for send enemies in scene show and event.phase == did. do remove event listeners for collision, touch, and enter frame for send enemies in scene destroy and hide and event.phase == did.

function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. Runtime:addEventListener("enterFrame", sendEnemies) Runtime:addEventListener("touch", touchScreen) Runtime:addEventListener("collision", onCollision) physics.start() end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene physics.pause() elseif phase == "did" then Runtime:removeEventListener("enterFrame", sendEnemies) Runtime:removeEventListener("touch", touchScreen) Runtime:removeEventListener("collision", onCollision) end end function scene:destroy( event ) Runtime:removeEventListener("enterFrame", sendEnemies) Runtime:removeEventListener("touch", touchScreen) Runtime:removeEventListener("collision", onCollision) -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. local sceneGroup = self.view end

I did this, but everytime the character hits an enemy they lose two lives, and occasionally there is still an enemy present after level1.lua has gone to menu.lua.

Similar to touch and scene you onColllison function has two phase began and ended. Because of this your player hit goes off twice

I don’t see anything about menu.lua in your code