Ok, so this is utterly frustrating me. I seem to be incapable of removing scenes. So, to test my ineptitude I made the simplest program I thought i could, 3 files, total. First, my “very long” main.lua:
----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer") -- Your code here composer.gotoScene("first\_scene")
the slightly longer first_scene.lua
local composer = require( "composer" ) local scene = composer.newScene() -- ----------------------------------------------------------------------------------------------------------------- -- All code outside of the listener functions will only be executed ONCE unless "composer.removeScene()" is called. -- ----------------------------------------------------------------------------------------------------------------- -- local forward references should go here -- ------------------------------------------------------------------------------- -- "scene:create()" function scene:create( event ) local sceneGroup = self.view -- create object local myObject = display.newRect( 0, 0, 100, 100 ) local secondObject = display.newRect( 100,100,50,50 ) local parent = display.newGroup( ) parent:insert( myObject ) parent:insert( secondObject ) myObject:setFillColor( 255 ) end -- "scene:show()" function scene:show( event ) composer.removeScene("previous") local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is on screen (but is about to go off screen). -- Insert code here to "pause" the scene. -- Example: stop timers, stop animation, stop audio, etc. elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's view ("sceneGroup"). -- Insert code here to clean up the scene. -- Example: remove display objects, save state, etc. end -- touch listener function function myObject:touch( event ) if event.phase == "began" then self.markX = self.x -- store x location of object self.markY = self.y -- store y location of object elseif event.phase == "moved" then local x = (event.x - event.xStart) + self.markX local y = (event.y - event.yStart) + self.markY parent.x, parent.y = x, y -- move object based on calculations above end return true end function myObject:tap(tap ) local currentscene = composer.getSceneName("current") print( currentscene ) local currentscene = "is this working" print( currentscene ) --composer.gotoScene("second\_scene") -- body end -- make 'myObject' listen for touch events myObject:addEventListener( "touch", myObject ) myObject:addEventListener( "tap", myObject ) -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene
There’s one more scene that I can try to go to, but it’s a moot point, because that the point i tap my output is this:
nil is this working
Where am I going wrong with all of this? All of the tutorials seems to make it sound so easy (just composer.removeScene()!) But I’m lost with it.