After making a small game to get used to Corona, I tried to implement a way to return to the title screen, but it isn’t working. A method called from native.showAlert leads to a composer.gotoscene statement, which does not work. Going to the title screen from the main.lua file works, and entering the game scene from the title screen file works, but entering the title screen from the game scene doesn’t.
title scene code:
----------------------------------------------------------------------------------------- -- -- menu.lua -- ----------------------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() -- include Corona's "widget" library local widget = require "widget" -------------------------------------------- -- forward declarations and other locals local playBtn -- 'onRelease' event listener for playBtn local function onPlayBtnRelease() composer.gotoScene("game\_scene") print("still here?") return true -- indicates successful touch end function scene:create( event ) local sceneGroup = self.view -- Called when the scene's view does not exist. -- -- INSERT code here to initialize the scene -- e.g. add display objects to 'sceneGroup', add touch listeners, etc. -- display a background image local background = display.newImageRect( "Background.png", display.actualContentWidth, display.actualContentHeight ) background.anchorX = 0 background.anchorY = 0 background.x = 0 + display.screenOriginX background.y = 0 + display.screenOriginY -- create/position logo/title image on upper-half of the screen local titleLogo = display.newImageRect( "logo.png", 264, 42 ) titleLogo.x = display.contentCenterX titleLogo.y = 100 -- create a widget button (which will loads level1.lua on release) playBtn = widget.newButton{ label="Play Now", labelColor = { default={255}, over={128} }, defaultFile="button.png", overFile="button-over.png", width=154, height=40, onRelease = onPlayBtnRelease -- event listener function } playBtn.x = display.contentCenterX playBtn.y = display.contentHeight - 125 -- all display objects must be inserted into group sceneGroup:insert( background ) sceneGroup:insert( titleLogo ) sceneGroup:insert( playBtn ) end function scene:show( event ) local sceneGroup = self.view local phase = event.phase if phase == "will" then -- Called when the scene is still off screen and is about to move on screen elseif phase == "did" then -- Called when the scene is now on screen -- -- INSERT code here to make the scene come alive -- e.g. start timers, begin animation, play audio, etc. end end function scene:hide( event ) local sceneGroup = self.view local phase = event.phase if event.phase == "will" then -- Called when the scene is on screen and is about to move off screen -- INSERT code here to pause the scene -- e.g. stop timers, stop animation, unload sounds, etc.) elseif phase == "did" then -- Called when the scene is now off screen if playBtn then playBtn:removeEventListener( "onRelease", onPlayBtnRelease ) end end end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. if playBtn then playBtn:removeSelf() -- widgets must be manually removed playBtn = nil end end --------------------------------------------------------------------------------- -- Listener setup scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) ----------------------------------------------------------------------------------------- return scene
game scene code:
-------------------------------------------------------------------------------- -- -- game\_scene.lua -- -------------------------------------------------------------------------------- local composer = require "composer" local widget = require "widget" local scene = composer.newScene() local physics = require( "physics" ) physics.start() -- Just some gui stuff. halfW = display.contentWidth \* 0.5 halfH = display.contentHeight \* 0.5 -- Displays an image local bkg = display.newImage( "Background.png", halfW, halfH) local saveFileName = "scoreSave.txt" local savePath = system.pathForFile(saveFileName, system.DocumentsDirectory) score = 0 scoreText = display.newText( score, halfW, 10 ) function SaveAlertCompleted(event) if (event.action == "clicked") then local i = event.index if(i == 1) then -- Player pressed OK elseif(i == 2) then -- Player pressed Quit Game composer.gotoScene("title\_scene") end end end local function saveProgress() local file = io.open(savePath, "w") -- w for writing, r for reading if(file) then file:write(score) io.close (file) local alert = native.showAlert( "Balloon Popper", "The game has been saved", { "OK", "Quit Game" }, SaveAlertCompleted ) return true else print("Error: ", saveFileName, " could not be written to.") return false end end local function loadProgress() local file = io.open(savePath, "r") if(file) then local contents = file:read("\*all") score = tonumber(contents) scoreText.text = score io.close(file) return true else print("Error: ", saveFileName, " could not be read.") return false end end saveButton = widget.newButton( { label="Save Game", labelColor = { default={255}, over={128} }, defaultFile="button.png", overFile="button-over.png", width=154, height=40, onRelease = saveProgress -- event listener function } ) saveButton.anchorX = 0 saveButton.anchorY = 0 saveButton.x = 0 saveButton.y = 0 -- TOUCH EVENT local function bombTouched (event) if( event.phase == "began") then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = math.floor(score \* 0.5) scoreText.text = score end end -- TOUCH EVENT local function balloonTouched(event) if ( event.phase == "began") then Runtime:removeEventListener( "enterFrame", event.self ) event.target:removeSelf() score = score + 1 scoreText.text = score end end -- ERROR CHECK/MEMORY SAVER local function offscreen (self, event) if(self.y == nil) then -- prevents a race event between the touched and the offscreen functions return end if(self.y\>display.contentHeight+ 50) then Runtime:removeEventListener( "enterFrame", self ) self:removeSelf( ) end end -- SPAWNS NEW OBJECT local function addNewBalloonOrBomb() local startX = math.random(display.contentWidth\*0.1,display.contentWidth\*0.9) if(math.random(1,5)==1) then local bomb = display.newImageRect( "bomb.png", display.contentHeight/2, display.contentHeight/2) bomb.x = startX bomb.y = -300 physics.addBody( bomb ) bomb.enterFrame = offscreen Runtime:addEventListener( "enterFrame", bomb ) bomb:addEventListener( "touch", bombTouched ) else local balloon = display.newImageRect( "balloon.png", display.contentHeight/2.5, display.contentHeight/2) balloon.x = startX balloon.y = -300 physics.addBody( balloon ) balloon.enterFrame = offscreen Runtime:addEventListener( "enterFrame", balloon ) balloon:addEventListener( "touch", balloonTouched ) end end addNewBalloonOrBomb() timer.performWithDelay( 500, addNewBalloonOrBomb, 0 ) function scene:hide( event ) end function scene:destroy( event ) local sceneGroup = self.view -- Called prior to the removal of scene's "view" (sceneGroup) -- -- INSERT code here to cleanup the scene -- e.g. remove display objects, remove touch listeners, save state, etc. if saveButton then saveButton:removeSelf() -- widgets must be manually removed saveButton = nil end end scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene
Sorry if this post was long, I’m not sure what to do.