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