Composer - scene won't hide

I’m trying to get my head around the scene concept and it seems no matter how simple one make things some idiot (me) manages not making it work.

I’ve tried to make a simple example where any click on the screen conjures up another scene that automatically closes after a second.

A print statement confirms that after a second scene:hide("did) is ran, but nothing happens. The scene is not hidden.

I also thought that since my options include a fade effect, that the second scene would somehow fade into view, but it shows instantly with no fade effect.

I obviously does something wrong, but what?

main.lua:

local w = display.contentWidth local h = display.contentHeight local widget   = require( "widget" ) local composer = require( "composer" ) local scene = composer.newScene() local options = {effect = "fade", time = 500 } local backgroundRect = display.newRect(w/2, h/2, w, h) backgroundRect:setFillColor(0.8) local touch = function(e) if (e.phase == "ended") then composer.showOverlay( "options", options ) end end backgroundRect:addEventListener( "touch", touch ) return scene

options.lua:

 

local composer = require( "composer" ) local scene = composer.newScene() local w = display.contentWidth local h = display.contentHeight local function closeMe() composer.hideOverlay( "fade", 400 ) end -- "scene:create()" function scene:create( event ) local sceneGroup = self.view local rect = display.newRect(w/2, h/2, w, h) rect:setFillColor(0.2) sceneGroup:insert(rect) local txt = display.newText("Hello World!", w/2, h/2, native.systemFont, 32 ) sceneGroup:insert(txt) end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then   elseif ( phase == "did" ) then timer.performWithDelay(1000, closeMe())   end end -- "scene:hide()" function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then print("scene:hide (did)") end end -- "scene:destroy()" function scene:destroy( event ) local sceneGroup = self.view end -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Hi runewinse,

you’ve forgotten to define your rectangle ‘rect’ and your text ‘txt’ as children of the parent displaygroup ‘sceneGroup’.

Add

sceneGroup:insert(rect) and

sceneGroup:insert(txt)

at the end of “scene:create” after you have made rect and txt.

What happens now is that the overlay (and the sceneGroup) hides but because rect and txt are not in that sceneGroup they are left on the screen.

Hmm… I’ve also tried that (I managed to try and update my original post before you answered!).

I’ve made the whole thing some lines smaller and changed the other scene to “prefs” instead of “options” (you never know).

But no luck. I’ve tried to add txt to the sceneGroup like this:

local txt = display.newText("Hello!", w/2, h/2, native.systemFont, 80 ) sceneGroup:insert(txt)

And I’ve tried to do it directly like this:

local txt = display.newText(sceneGroup, "Hello!", w/2, h/2, native.systemFont, 80 )

But if I do any of the two versions above, the scene simply doesn’t show at all…  :huh:

The complete code:

main.lua:

local w = display.contentWidth local h = display.contentHeight local widget   = require( "widget" ) local composer = require( "composer" ) local scene = composer.newScene() local options = {effect = "fade", time = 500 } local backgroundRect = display.newRect(w/2, h/2, w, h) backgroundRect:setFillColor(0.0) local touch = function(e) if (e.phase == "ended") then composer.showOverlay( "prefs", options ) end end backgroundRect:addEventListener( "touch", touch ) return scene

prefs.lua:

local composer = require( "composer" ) local scene = composer.newScene() local w = display.contentWidth local h = display.contentHeight local function closeMe() composer.hideOverlay( "fade", 400 ) end function scene:create( event ) local sceneGroup = self.view local txt = display.newText("Hello!", w/2, h/2, native.systemFont, 80 ) sceneGroup:insert(txt) end function scene:show( event ) if ( event.phase == "did" ) then timer.performWithDelay(1000, closeMe()) end end function scene:hide( event ) if ( event.phase == "did" ) then print("scene:hide (did)") end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Hi runewinse,

you’ve forgotten to define your rectangle ‘rect’ and your text ‘txt’ as children of the parent displaygroup ‘sceneGroup’.

Add

sceneGroup:insert(rect) and

sceneGroup:insert(txt)

at the end of “scene:create” after you have made rect and txt.

What happens now is that the overlay (and the sceneGroup) hides but because rect and txt are not in that sceneGroup they are left on the screen.

Hmm… I’ve also tried that (I managed to try and update my original post before you answered!).

I’ve made the whole thing some lines smaller and changed the other scene to “prefs” instead of “options” (you never know).

But no luck. I’ve tried to add txt to the sceneGroup like this:

local txt = display.newText("Hello!", w/2, h/2, native.systemFont, 80 ) sceneGroup:insert(txt)

And I’ve tried to do it directly like this:

local txt = display.newText(sceneGroup, "Hello!", w/2, h/2, native.systemFont, 80 )

But if I do any of the two versions above, the scene simply doesn’t show at all…  :huh:

The complete code:

main.lua:

local w = display.contentWidth local h = display.contentHeight local widget   = require( "widget" ) local composer = require( "composer" ) local scene = composer.newScene() local options = {effect = "fade", time = 500 } local backgroundRect = display.newRect(w/2, h/2, w, h) backgroundRect:setFillColor(0.0) local touch = function(e) if (e.phase == "ended") then composer.showOverlay( "prefs", options ) end end backgroundRect:addEventListener( "touch", touch ) return scene

prefs.lua:

local composer = require( "composer" ) local scene = composer.newScene() local w = display.contentWidth local h = display.contentHeight local function closeMe() composer.hideOverlay( "fade", 400 ) end function scene:create( event ) local sceneGroup = self.view local txt = display.newText("Hello!", w/2, h/2, native.systemFont, 80 ) sceneGroup:insert(txt) end function scene:show( event ) if ( event.phase == "did" ) then timer.performWithDelay(1000, closeMe()) end end function scene:hide( event ) if ( event.phase == "did" ) then print("scene:hide (did)") end end function scene:destroy( event ) local sceneGroup = self.view end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene