Why can I never seem to delete a scene I create?

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.

I think If a scene is removed it must be re-created.

If the scene that is removed  does not  have an associated module, then the scene object will be removed (and must be re-created if it is to be loaded again).

What do you mean by remove scene? Remove from the screen or from memory?

If from screen, you are not inserting your parent group into the sceneGroup, so composer will not remove it automatically.

sometimes i feel like an idiot. Thanks Nick that nailed it on the head.

Edit: Now, is this going to let me reset the scene all the way back to the beginning so everything gets re-created when I come back to it?

While this was test code, the actual application I was working with is a level select map, so I need to make sure everything get’s re-drawn to account for the possible new information, and for the most part, everything is being called from scene:create()

Whack this in the “did” phase of your scene:show function on each scene, and it will clear the previous scene from memory so it starts from scratch when you return to it.

[lua]

local previous =  composer.getSceneName( “previous” )

      if previous ~= “main” and previous then

          composer.removeScene(previous, false)       – remove previous scene from memory

      end

[/lua]

-- "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

 

At what point do you insert your objects into the sceneGroup?  You can either put parent in sceneGroup or skip the parent and put the objects in the sceneGroup.  Objects not in the sceneGroup will **not** be managed by Composer.

 

This is very likely the issue.

 

On a side note, Colors in Corona range from 0 to 1, not 0 to 255.  Your set fill color will work because its greater than 0.99999 and clips to 1.  (Unless for some reason you’re trying to use Graphics 1.0 compatibility mode.)

I think If a scene is removed it must be re-created.

If the scene that is removed  does not  have an associated module, then the scene object will be removed (and must be re-created if it is to be loaded again).

What do you mean by remove scene? Remove from the screen or from memory?

If from screen, you are not inserting your parent group into the sceneGroup, so composer will not remove it automatically.

sometimes i feel like an idiot. Thanks Nick that nailed it on the head.

Edit: Now, is this going to let me reset the scene all the way back to the beginning so everything gets re-created when I come back to it?

While this was test code, the actual application I was working with is a level select map, so I need to make sure everything get’s re-drawn to account for the possible new information, and for the most part, everything is being called from scene:create()

Whack this in the “did” phase of your scene:show function on each scene, and it will clear the previous scene from memory so it starts from scratch when you return to it.

[lua]

local previous =  composer.getSceneName( “previous” )

      if previous ~= “main” and previous then

          composer.removeScene(previous, false)       – remove previous scene from memory

      end

[/lua]

-- "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

 

At what point do you insert your objects into the sceneGroup?  You can either put parent in sceneGroup or skip the parent and put the objects in the sceneGroup.  Objects not in the sceneGroup will **not** be managed by Composer.

 

This is very likely the issue.

 

On a side note, Colors in Corona range from 0 to 1, not 0 to 255.  Your set fill color will work because its greater than 0.99999 and clips to 1.  (Unless for some reason you’re trying to use Graphics 1.0 compatibility mode.)