I don’t reload scenes this way, but in trying to explain why I don’t, I ran into a problem that has me stumped. I’m not sure why I’m seeing the results I’m getting.
Basically, I have a Retry button on the scene and it calls gotoScene on itself. It appears to work the first time, but then doesn’t after that – although it appears gotoScene is being called each time I press the button.
I’d love to know what’s going on so I can say more than just “don’t do this” – I want to be able to explain why.
Below is the code for the composer scene – if you want to run it, just create a main.lua that calls this scene directly. I’ve replaced all the images with shapes so it should work fine with no other assets needed. I’ve also deleted most everything else to make the code shorter.
local composer = require( "composer" ) local scene = composer.newScene() local widget = require("widget") widget.setTheme ( "widget\_theme\_ios" ) print("--==\> play scene entered") local planet local retryBtn local centerX = display.contentCenterX local centerY = display.contentCenterY local screenLeft = display.screenOriginX local screenWidth = display.viewableContentWidth - screenLeft \* 2 local screenRight = screenLeft + screenWidth local screenTop = display.screenOriginY local screenHeight = display.viewableContentHeight - screenTop \* 2 local screenBottom = screenTop + screenHeight -- ------------------------------------------------------------------------------- local function startGame() print("...inside startGame") local text = display.newText( scene.view, "Tap here to start. Protect the planet!", 0, 0, "Helvetica", 24 ) text.x = centerX text.y = screenTop + 30 text:setFillColor(0, 0.02, 0.29) retryBtn.alpha = 1 end local function createPlayScreen() print("...inside createPlayScreen") planet = display.newCircle(scene.view, centerX, screenBottom+60, 30 ) planet:setFillColor(0,0,1) planet.alpha = 0 transition.to( planet, { time=2000, alpha=1, y=centerY, onComplete=startGame } ) end local function goSomewhere(event) print("...should be going to", event.target.id) local goto = event.target.id composer.gotoScene( goto, {effect="crossFade", time=500} ) return true end local function setUpDisplay(grp) print("...inside setUpDisplay") local bg = display.newRect(centerX, centerY, screenWidth, screenHeight) bg:setFillColor(0.94, 0.91, 1) grp:insert(bg) retryBtn = widget.newButton ( {width=60, height=30, label="Retry", id="play", onRelease=goSomewhere} ) retryBtn.anchorX = 1 retryBtn.anchorY = 1 retryBtn.x = screenRight - 10 retryBtn.y = screenBottom - 10 retryBtn.alpha = 0 grp:insert(retryBtn) end -- "scene:create()" function scene:create( event ) print("play scene:create") setUpDisplay(self.view) end -- "scene:show()" function scene:show( event ) local phase = event.phase if ( phase == "will" ) then print("play scene:show will") elseif ( phase == "did" ) then print("play scene:show did") createPlayScreen() end end -- "scene:hide()" function scene:hide( event ) local phase = event.phase if ( phase == "will" ) then print("play scene:hide will") elseif ( phase == "did" ) then print("play scene:hide did") composer.removeScene( "play" ) end end -- "scene:destroy()" function scene:destroy( event ) print("play scene:destroy") end -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
I didn’t expect the Retry button to work at all. I was surprised when it worked the first time, and then stumped when it didn’t work subsequent times.
Jay