Hello,
I seem to be having a problem with my game scene in that it does not seem to use composer i.e. I set up a back button that returns to the “start” page but it appears to not work.
Here is the code
local sceneName = ... local composer = require( "composer" ) local physics = require("physics") local widget = require("widget") local scene = composer.newScene(sceneName) ----------------------------------------------------------------------------------------- -- -- main.lua -- ----------------------------------------------------------------------------------------- -- ----------------------------------------------------------------------------------- -- Scene event functions -- ----------------------------------------------------------------------------------- -- create() function scene:create( event ) local sceneGroup = self.view display.setStatusBar(display.HiddenStatusBar) screenWidth = display.contentWidth screenHeight = display.contentHeight local background = display.newImageRect( "background.png", 360, 570 ) background.x = display.contentCenterX background.y = display.contentCenterY physics.start(true) physics.setGravity(0,50); local theWord = "" -- the word the user spells local theWordText -- shows the word the user writes local isNewGame local allBallsGroup -- group to hold all the balls local wordTable = {} -- will hold the words from the text file local chosenBalls = {} -- holds the balls the user has chosen local instructionsText -- shows instructions local gameOverText -- shows game over local countDownText -- shows the time local numBallsToGenerate = 10 -- how many balls to generate local allBalls = {} -- holds all the balls that have been generated local theBombs = {} -- holds references to the bomb image local generateBallTimer -- timer for generating balls local clockTimer -- timer for the clock local gameTime = 20 -- how many seconds before new balls drop function Setup() math.randomseed(os.time()) --gameOverText.text="" SetupGameAssets() StartButton() --gameOverText.text = nil end function StartButton() startButton = widget.newButton({ label = "START", labelColor = { default={ 0.1,0.5,0.2 }, over={ 0.1,0.5,0.2, 0.7 }}, fontSize = 25, onRelease = StartButtonPress, shape = "circle", width = 200, height = 40, cornerRadius = 2, radius = 60, fillColor = {default={0.6,1,0.7,0}, over={0.6,1,0.7,0}}, strokeColor = {default ={0.1,0.5,0.2,0.7}, over={0.1,0.5,0.2,0.5}}, strokeWidth = 8}) startButton.x = display.contentCenterX startButton.y = 200 returnMenuButton = widget.newButton({ label = "Back to Menu", labelColor = { default={ 0.1,0,0.5 }, over={ 0.1,0,0.5, 0.7 }}, fontSize = 20, onRelease = MenuButtonPress, shape = "roundedRect", width = 160, height = 40, cornerRadius = 2, fillColor = {default={0.6,1,0.7,0}, over={0.6,1,0.7,0}}, strokeColor = {default ={0.1,0,0.5,0.7}, over={0.1,0,0.5,0.5}}, strokeWidth = 8}) returnMenuButton.x = display.contentCenterX returnMenuButton.y = 340 --gameOverText:removeSelf() end -- show() back button function backButton() backbutton = widget.newButton( { width = 45, height = 45, onEvent = backbuttonEvent1, emboss = false, defaultFile = "arrow.png", overFile = "arrowover.png", } ) --back button bottom left backbutton.x = display.contentCenterX\*1.8 backbutton.y = display.contentCenterY\*0.13 end function StartButtonPress(event) startButton.isVisible = false returnMenuButton.isVisible = false gameOverText.text = "" NewGame() end function returnMenuButton( event ) if ( "ended" == event.phase ) then composer.gotoScene("start") end end -- back button function local function backbuttonEvent1( event ) if ( "ended" == event.phase ) then composer.gotoScene("start") end end function NewGame() isNewGame = true chosenBalls = {} allBalls = {} theWord = "" theWordText.text = "" instructionsText.text = "" gameOverText.text = "" countDownText.text = gameTime; CreateVowelBalls(2) CreateSpecialChar(2) GenerateBalls() StartTimer() isNewGame = false SetBombsVisible(); gameOverText.isVisible = false; backButton() end function SetupGameBoundaries() local groundLine = display.newRect(0, 430,display.contentWidth+330, 2); local lineLeft = display.newRect(-0,0,2,display.contentHeight+400); local lineRight = display.newRect(display.contentWidth,0,2,display.contentHeight+400) physics.addBody(groundLine, 'static',{bounce=0,friction=0}) physics.addBody(lineLeft, 'static',{bounce=0,friction=0}); physics.addBody(lineRight,'static',{bounce=0,friction=0}); groundLine.isVisible = false; lineLeft.isVisible = false; lineRight.isVisible = false; end function SetupButtons() local goButton = display.newImage("goButton.png",280,465); goButton:addEventListener('tap', CheckWord); local stopButton = display.newImage("stopButton.png",30,465); stopButton:addEventListener('tap',ResetWord) local bar = display.newImage("bar.png",0,100) bar.x = display.contentCenterX bar.y = display.contentHeight-400 end function SetupTextFields() countDownText = display.newText(gameTime,290,5,native.systemFontBold,20); countDownText:setTextColor("#000000"); theWordText = display.newText("",60,470,native.systemFontBold,25); theWordText:setTextColor("#000000"); instructionsText = display.newText("",0,0,native.systemFontBold,25); instructionsText.x = display.contentWidth/2; instructionsText.y = display.contentHeight/10; instructionsText:setTextColor("19007f"); end function SetupGameOverField() gameOverText = display.newText("",0,0,native.systemFontBold,25); gameOverText.x = display.contentCenterX; gameOverText.y = display.contentCenterY; gameOverText:setTextColor("19007f"); gameOverText.isVisible = false; end function SetupGameAssets() ReadTextFile() SetupGameBoundaries() SetupButtons() SetupTextFields() SetupGameOverField() SetupBombs() allBallsGroup = display.newGroup(); end function SetupBombs() for i=1, 5 do local tempBomb = display.newImage("bomb.png") tempBomb.width = 30 tempBomb.height = 30 tempBomb.x = 33 \* i tempBomb.y = 0 tempBomb:addEventListener('tap', Explode) table.insert(theBombs,tempBomb) end end function SetBombsVisible() for i=1, #theBombs do theBombs[i].isVisible = true; end end function SetBombsInvisible() for i=1, #theBombs do theBombs[i].isVisible = false; end end function GenerateBalls() timer.performWithDelay(1500,CheckGameOver) if(isNewGame == true) then numBallsToGenerate = 10 else numBallsToGenerate = 4 CreateVowelBalls(1) --CreateSpecialChar(1) end generateBallTimer = timer.performWithDelay( 50, CreateBall, numBallsToGenerate) end function StartTimer() clockTimer = timer.performWithDelay(1000,DoCountdown,gameTime) end function DoCountdown() local currentTime = countDownText.text currentTime = currentTime -1 countDownText.text = currentTime if(currentTime == 0) then GenerateBalls() countDownText.text = gameTime StartTimer() end end function CreateBall(createVowel) -- creates one ball. Takes one argument, the letter the ball contains local var alphabetArray = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"} local vowels = {"A", "E", "I", "O", "U",} local specialChar = {"Á", "É", "Í", "Ó", "Ú", "Ñ"} local ballType = math.random(4) local ballSize = math.random(2) local letterIndex local letter if(createVowel == true) then letterIndex = math.random(#vowels) letter = vowels[letterIndex] letterIndex = math.random(#specialChar); letter = specialChar[letterIndex] else letterIndex = math.random(#alphabetArray) letter = alphabetArray[letterIndex] end local ballGroup = display.newGroup(); local ball local ballRadius if(ballType == 1) then ball = display.newImage("greenBall.png") elseif(ballType == 2) then ball = display.newImage("brownBall.png") elseif(ballType == 3) then ball = display.newImage("pinkBall.png") else ball = display.newImage("redBall.png") end if(ballSize == 1)then ball.width = 48 ball.height = 48 ballRadius = 24 else ball.width = 58 ball.height = 58 ballRadius = 29 end local letterText = display.newText( letter, 0,0, native.systemFontBold, 25 ); letterText:setTextColor(0,0, 0) letterText.x = ball.x letterText.y = ball.y ballGroup:insert(ball) ballGroup:insert(letterText) ballGroup.x = math.random(ballRadius,display.contentWidth-ballRadius\*2) ballGroup.y= - 40 physics.addBody(ballGroup, 'dynamic',{friction = 0,bounce = 0,radius = ballRadius}) ballGroup.name = "ball" ballGroup.letter = letter ballGroup:addEventListener('tap',FormString) table.insert(allBalls,ballGroup) allBallsGroup:insert(ballGroup) end function CheckGameOver () local gameOver = false; for i=1,#allBalls do if(allBalls[i].y \< (100 - allBalls[i].height))then gameOver = true; break; end end if(gameOver) then for i=allBallsGroup.numChildren,1,-1 do local child = allBallsGroup[i] child:removeSelf() child = nil; end timer.cancel(generateBallTimer) timer.cancel(clockTimer) SetupGameOverField() gameOverText.text = "Game Over"; --gameOverText:toFront() SetBombsInvisible() theWordText.isVisible = false; countDownText.isVisible = false; gameOverText.isVisible = false; --transition.to (gameOverText.text, { time=2000, alpha=0 }); timer.performWithDelay(2000, Setup); end end function CheckWord() if(#theWord \<= 1) then return; end local lowerCaseWord = string.lower(theWord) local tempBall if(table.indexOf(wordTable,lowerCaseWord) == nil) then --print(theWord) instructionsText.text = "NOT A WORD!"; instructionsText:toFront() else for i=1, #chosenBalls do table.remove(allBalls,table.indexOf(allBalls,chosenBalls[i])) chosenBalls[i]:removeSelf(); chosenBalls[i] = nil; theWord = "" theWordText.text = "" end print (#allBalls) chosenBalls = {} end end function ResetWord() instructionsText.text = ""; theWord = '' theWordText.text = "" chosenBalls = {} end function CreateVowelBalls(number) for i=1, number do CreateBall(true) end end function CreateSpecialChar(number) for i=1, number do CreateBall(true) end end function FormString(e) local thisSprite = e.target local theLetter = thisSprite.letter; --if(table.indexOf(chosenBalls,thisSprite) == nil) then table.insert(chosenBalls,thisSprite); theWord = theWord .. theLetter; theWordText.text = theWord; theWordText.x = display.contentWidth/2; --else --print("You already chose that ball silly"); --end end function Explode(e) local thisSprite = e.target thisSprite.isVisible = false local randomIndex local randomBall if(#allBalls \< 5) then for i=1, #allBalls do RemoveBall() end else for i=1, 5 do RemoveBall() end end print(#allBalls) end function RemoveBall() local randomIndex = math.random(#allBalls) local tempBall = allBalls[randomIndex] tempBall:removeSelf() tempBall = nil table.remove(allBalls,randomIndex); end function ReadTextFile() local path = system.pathForFile( "espanol2utf8.txt", system.ResourceDirectory) local file = io.open( path, "r" ) for line in file:lines() do line = string.gsub(line, 1, #line - 1); table.insert(wordTable,line) end io.close( file ) file = nil end Setup() end -- "scene:show()" function scene:show( event ) local sceneGroup = self.view local phase = event.phase if ( phase == "will" ) then -- Called when the scene is still off screen (but is about to come on screen). elseif ( phase == "did" ) then -- Called when the scene is now on screen. -- Insert code here to make the scene come alive. -- Example: start timers, begin animation, play audio, etc. 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
Any thoughts or suggestions?