Switching between two scenes is not working

I am trying to switch between two scenes.But after two switches,it doesn’t work as expected

My code

main.lua

local composer=require"composer" composer.gotoScene("scene1")

scene1.lua

local composer = require( "composer" ) local scene = composer.newScene() local xc=display.contentCenterX local yc=display.contentCenterY local w=display.contentWidth local h=display.contentHeight function scene:create( event ) local sg=self.view -- local perspective=require"perspective" -- local camera=perspective.createView() local base=display.newRect(sg,0,yc,100,20) base:setFillColor( 0,1,0 ) sg:insert(base) -- camera:add(base,1,true) local function scene2(event) if event.phase=="began" then print( "going to scene2" ) Runtime:removeEventListener( "touch",scene2) composer.gotoScene("scene2") -- camera:destroy() end end Runtime:addEventListener( "touch",scene2) end function scene:show( event ) end function scene:hide( event ) if event.phase=="did " then --composer.removeScene("scene1") end end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

scene2.lua

local composer = require( "composer" ) local scene = composer.newScene() -- local perspective=require"perspective" -- local camera=perspective.createView() local xc=display.contentCenterX local yc=display.contentCenterY local w=display.contentWidth local h=display.contentHeight function scene:create( event ) local perspective=require"perspective" -- local camera=perspective.createView() local sg=self.view local base=display.newRect(sg,0,yc,100,20) base:setFillColor( 1,0,0 ) sg:insert(base) -- camera:add(base,1,true) local function scene1(event) if event.phase=="began" then print( "going to scene1" ) Runtime:removeEventListener( "touch",scene1) composer.gotoScene("scene1") -- camera:destroy() end end Runtime:addEventListener( "touch",scene1) end function scene:show( event ) end function scene:hide( event ) if event.phase=="did " then --composer.removeScene("scene2") end end function scene:destroy( event ) end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

What is wrong with the above code?

Hi @DevilDev,

What is wrong with the above code?

The code don’t work as you expected because create function is called only once during creation of scene. If you  don’t remove scene it stays in memory.

Read more 

Have a nice day:)

ldurniat 

@DD,  pretty good post, but you forgot one detail.  You said it didn’t work as ‘expected’, but you didn’t list/write/outline what you actually expected.

@ldurniat figured out what your implied expect action was, but you probably want to be explicit in future posts.  i.e. The good old list:

  1. I did this.
  2. I saw this.
  3. I expected to see this.
  4. This is why I think #2 is wrong.  (Not the same as #3;  This is your interpretation and cogitation, not expectation.)
  5. Bonus: This is what I did to debug it/investigate.

#4 above is the hardest to write and it is usually when trying to explain that bit that folks work out why #2 and #3 don’t match.

Hi @DevilDev,

What is wrong with the above code?

The code don’t work as you expected because create function is called only once during creation of scene. If you  don’t remove scene it stays in memory.

Read more 

Have a nice day:)

ldurniat 

@DD,  pretty good post, but you forgot one detail.  You said it didn’t work as ‘expected’, but you didn’t list/write/outline what you actually expected.

@ldurniat figured out what your implied expect action was, but you probably want to be explicit in future posts.  i.e. The good old list:

  1. I did this.
  2. I saw this.
  3. I expected to see this.
  4. This is why I think #2 is wrong.  (Not the same as #3;  This is your interpretation and cogitation, not expectation.)
  5. Bonus: This is what I did to debug it/investigate.

#4 above is the hardest to write and it is usually when trying to explain that bit that folks work out why #2 and #3 don’t match.