You have several things fighting you on this. First, its really important if you’re going to ask for help to format your code well. You have un-indented code making it a challenge for people to understand the flow. It also makes it hard for you to understand the flow. Please read this tutorial:
https://docs.coronalabs.com/tutorial/basics/codeFormatting/index.html
Now on to other issues.
local function gotoTest() composer.gotoScene("scene2") composer.removeScene(self) Runtime:removeEventListener("enterFrame",gameLoop1) end
First, I don’t recommend doing work after going to another scene. In this case, if the removeScene worked (which it probably partially did) the code is destroyed and there is a good chance your removeEventListener might not work. Secondly you really should not try and remove the scene you are in. The only safe time to do that is in scene:hide() during the “did” phase. Cancelling runtime listeners is also recommended to be done in scene:hide() during the “will” phase. But it’s perfectly valid to cancel them before you call gotoScene. I would rewrite that block of code to be:
local function gotoTest() Runtime:removeEventListener("enterFrame",gameLoop1) composer.gotoScene("scene2") end
Then make your scene:hide() :
function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen composer.removeScene("thesceenname") end end
You can’t pass self to the removeScene() API, it’s expecting the name of the scene. Since you know the name of your scene, just hard code it.
Finally you have to watch for scope issues. The function gameLoop1 is only known about inside of scene:create(). If you simply move that function outside of scene:create(), then gameLoop1 will be known scene wide and then you could start and stop your enterFrame listener at the right time. If you have any transition time on the scene, lets say you fade the new scene in over 500ms then that means that your enterFrame will be running for 500ms before the user ever sees the screen. It’s always best to defer actions that start activities until the scene is fully on the screen. Consider this instead of what you have:
local composer = require( "composer" ) local scene = composer.newScene() ------------------------------- -- Put your functions here: local function gameLoop1() print("On Scene 1") end local function gotoTest() composer.gotoScene("scene2") end function scene:create( event ) local sceneGroup = self.view local circle = display.newCircle(display.contentCenterX,display.contentCenterY,40) circle:addEventListener("tap",gotoTest) sceneGroup:insert(circle) local text = display.newText("1",circle.x,circle.y,native.systemFont,40) text:setFillColor(0) sceneGroup:insert(text) 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 Runtime:addEventListener("enterFrame",gameLoop1) end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) Runtime:removeEventListener("enterFrame",gameLoop1) elseif phase == "did" then -- Called when the scene is now off screen composer.removeScene("scene1") end end
Rob