Cannot go to a specific scene

Hi, so I’m creating a button which leads to the menu scene on click, but it doesn’t seem to work, I mean the scene doesn’t change, it stays on the current one, however I can see in console that the button’s phase ends and does what it does to the button, but doesn’t move to the menu scene, here’s the code:

 

local backBtn = display.newRect(w, h+80, 100, 50) local function onBackClick(event) local t = event.target if event.phase == "began" then display.getCurrentStage():setFocus( t ) t.isFocus = true t.yScale = 0.9 t.xScale = 0.9 t.alpha = 0.8 print("backBtn phase = began") elseif t.isFocus then if event.phase == "ended" then print("backBtn phase = ended") display.getCurrentStage():setFocus( nil ) t.isFocus = false t.yScale = 1 t.xScale = 1 t.alpha = 1 composer.gotoScene("menu") end end end backBtn:addEventListener("touch", onBackClick)

The code works for me … the only addition I made was ‘local composer = require( “composer” )’ at the top; I can see it enter the scene:show function in menu.lua.

Does the console show anything other than “backBtn phase = ended” ? 

Of course I have required the composer, here’s the full code for the scene anyway, it’s pretty annoying I cannot get it fixed…
 

----------------------------------------------------------------------------------------- -- -- searchScene.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() function searchForNumber() local rect = display.newRect(w, h, display.actualContentWidth, display.actualContentHeight) rect:setFillColor(0.5, 0.5, 0.5) rect.alpha = 1 local phono = getPhoneNumber() or "123123" local searchingText = display.newText("Searching for " .. phono .. ".", w, h, native.systemFont, 16 ) local sLength = phono:len() local dot = true local sText = searchingText.text print(sText) --print(" THIS " .. sText:sub( 16 + tonumber( sLength ) )) local function animateText() if(dot) then searchingText.text = "Searching for " .. phono .. twoDots .. "" dot = false else searchingText.text = "Searching for " .. phono .. oneDot .. "" dot = true end end searchTimer = timer.performWithDelay(500, animateText, -1) local function numberFound() timer.cancel( searchTimer ) searchingText:removeSelf() searchingText = nil local rect = display.newRect(w, h, 300, 250) rect:setFillColor(0.7, 0.7, 0.7) location = "USA, New York" local text = display.newText("Number found!", w, h-100, native.systemFont, 26) local location = display.newText("Location: " .. location .. "", w, h, native.systemFont, 20) local backBtn = display.newRect(w, h+80, 100, 50) local function onBackClick(event) local t = event.target if event.phase == "began" then display.getCurrentStage():setFocus( t ) t.isFocus = true t.yScale = 0.9 t.xScale = 0.9 t.alpha = 0.8 elseif t.isFocus then if event.phase == "ended" then display.getCurrentStage():setFocus( nil ) t.isFocus = false t.yScale = 1 t.xScale = 1 t.alpha = 1 composer.gotoScene("menu") end end end backBtn:addEventListener("touch", onBackClick) end timer.performWithDelay(500 + math.random(400, 500), numberFound) end function scene:create( event ) local sceneGroup = self.view local rect = display.newRect(w, h, display.actualContentWidth, display.actualContentHeight) rect:setFillColor(0.5, 0.5, 0.5) searchForNumber() end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then 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 -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

This is a simple case that you are not inserting your display objects into the scene’s group.  Composer works by using a display.newGroup() to handle transitioning your scene on and off screen and to automatically clean the scene up.  If you do not insert the objects into the group, Composer cannot work for you.

When you create a new object like say a rectangle inside one of the scene event functions, you do:

local myRect = display.newRect( 100, 100, 50, 50 ) sceneGroup:insert( myRect )   --- this is the important statement that makes composer work....

If you need to do the same thing, but not inside of one of the scene event functions, then you need to do this:

local myRect = display.newRect( 100, 100, 50, 50 ) scene.view:insert( myRect )

Rob

The code works for me … the only addition I made was ‘local composer = require( “composer” )’ at the top; I can see it enter the scene:show function in menu.lua.

Does the console show anything other than “backBtn phase = ended” ? 

Of course I have required the composer, here’s the full code for the scene anyway, it’s pretty annoying I cannot get it fixed…
 

----------------------------------------------------------------------------------------- -- -- searchScene.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() function searchForNumber() local rect = display.newRect(w, h, display.actualContentWidth, display.actualContentHeight) rect:setFillColor(0.5, 0.5, 0.5) rect.alpha = 1 local phono = getPhoneNumber() or "123123" local searchingText = display.newText("Searching for " .. phono .. ".", w, h, native.systemFont, 16 ) local sLength = phono:len() local dot = true local sText = searchingText.text print(sText) --print(" THIS " .. sText:sub( 16 + tonumber( sLength ) )) local function animateText() if(dot) then searchingText.text = "Searching for " .. phono .. twoDots .. "" dot = false else searchingText.text = "Searching for " .. phono .. oneDot .. "" dot = true end end searchTimer = timer.performWithDelay(500, animateText, -1) local function numberFound() timer.cancel( searchTimer ) searchingText:removeSelf() searchingText = nil local rect = display.newRect(w, h, 300, 250) rect:setFillColor(0.7, 0.7, 0.7) location = "USA, New York" local text = display.newText("Number found!", w, h-100, native.systemFont, 26) local location = display.newText("Location: " .. location .. "", w, h, native.systemFont, 20) local backBtn = display.newRect(w, h+80, 100, 50) local function onBackClick(event) local t = event.target if event.phase == "began" then display.getCurrentStage():setFocus( t ) t.isFocus = true t.yScale = 0.9 t.xScale = 0.9 t.alpha = 0.8 elseif t.isFocus then if event.phase == "ended" then display.getCurrentStage():setFocus( nil ) t.isFocus = false t.yScale = 1 t.xScale = 1 t.alpha = 1 composer.gotoScene("menu") end end end backBtn:addEventListener("touch", onBackClick) end timer.performWithDelay(500 + math.random(400, 500), numberFound) end function scene:create( event ) local sceneGroup = self.view local rect = display.newRect(w, h, display.actualContentWidth, display.actualContentHeight) rect:setFillColor(0.5, 0.5, 0.5) searchForNumber() end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then elseif ( phase == "did" ) then 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 -- ------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) -- ------------------------------------------------------------------------------- return scene

This is a simple case that you are not inserting your display objects into the scene’s group.  Composer works by using a display.newGroup() to handle transitioning your scene on and off screen and to automatically clean the scene up.  If you do not insert the objects into the group, Composer cannot work for you.

When you create a new object like say a rectangle inside one of the scene event functions, you do:

local myRect = display.newRect( 100, 100, 50, 50 ) sceneGroup:insert( myRect )   --- this is the important statement that makes composer work....

If you need to do the same thing, but not inside of one of the scene event functions, then you need to do this:

local myRect = display.newRect( 100, 100, 50, 50 ) scene.view:insert( myRect )

Rob