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?