Timer is incrementing by 1?

The situation is i have a a countUp timer that stars from 0 to up at first the counts is 1,2,3,4 and after going to my menu and going back to that scene what happens is 2,4,6,8 and 3,6,9,12 this is what happens everytime i go back to the scene.

gameplay with Timer Scene

local composer = require( "composer" ) local scene = composer.newScene() local \_W = display.contentWidth local \_H = display.contentHeight local mF = math.floor local mR = math.random local widget = require "widget" -- Load our utils and main categories local utils = require("helpers.globals") local categories = require("helpers.additionDifficulty") local averageFont = "fonts/Sketch3D" -- Display Groups local screenGroup local quizGroup -- Holds the quiz UI etc local uiGroup -- Holds the navBar on other UI local overGroup -- Only created when we tap on the pause button or get gameover -- Game variables local category = {} -- The chosen category once loaded. local questions = {} -- The category quiz questions once loaded. local colour = {} -- Category colour once loaded local onCategory = 0 -- Tracks the category we are on (used for gameOver) local onQuestion = 0 -- Tracks the question we are on local currentScore = 0 -- The player's score local timePerQuestion = 0 -- Set in categories.lua. 0 removes the timer. local timerEnabled = true -- Stops or allows our countdown timer, based off timePerQuestion local touchEnabled = false -- Stops button touches difficulty = 12 -- Transitions and timers local delay\_timer -- Game over delay for changing scene local timer\_trans -- Transition for our timer local result\_trans -- Transition for "Correct!" local countUpText local clockTimer local gameTime = 0 -- Display objects local over\_rect -- Pre-declare so we can alter it from the day/night button as well. local question\_rect -- Pre-declare so we can access anywhere local timer\_rect local timer\_bar -- The countdown bar local label\_question -- The question number label local label\_result -- The "Correct" text label local answer\_rect -- Functions local updateScore local startTimer local gameOver local buttonTouched local createQuestion --variable for pause local pauseButton local pauseScreen ----------------------------------------------- --\*\*\* OTHER FUNCTIONS \*\*\* ----------------------------------------------- -- Button touched buttonTouched = function(event) local t = event.target local id = t.id if event.phase == "began" and touchEnabled == true then display.getCurrentStage():setFocus( t ) t.isFocus = true if id == "answer" then t.alpha = 0.6 else t.xScale = 0.9 t.yScale = 0.9 end elseif t.isFocus then if event.phase == "ended" then display.getCurrentStage():setFocus( nil ) t.isFocus = false if id == "answer" then t.alpha = 1 else t.xScale = 1 t.yScale = 1 end -- Check that touch finished in the button. local b = t.contentBounds if event.x \>= b.xMin and event.x \<= b.xMax and event.y \>= b.yMin and event.y \<= b.yMax then utils.playSound("select") if id == "answer" then if timer\_trans ~= nil then transition.cancel(timer\_trans) timer\_trans = nil end if result\_trans ~= nil then transition.cancel(result\_trans) result\_trans = nil end if label\_result ~= nil then display.remove(label\_result) label\_result = nil end -- Show some text that we can transition label\_result = display.newText({parent=uiGroup, text="", font=averageFont, fontSize=75}) label\_result.anchorX = 0 label\_result.x = label\_question.x - 540 label\_result.y = label\_question.y + 400 if t.index == questions[onQuestion].answer then label\_result.text = "Correct!" label\_result:setFillColor(0,0.6,0) utils.playSound("score") updateScore(1) else label\_result.text = "Incorrect..." label\_result:setFillColor(0.8,0,0) utils.playSound("incorrect") end result\_trans = transition.to(label\_result, {time=1600, alpha=0.1, y=label\_result.y-18, onComplete=function() display.remove(label\_result) label\_result = nil end}) -- Now create the next quesiton createQuestion() timer.cancel(clockTimer) end end end end return true end -- End the game gameOver = function() touchEnabled = false utils.playSound("gameover") delay\_timer = timer.performWithDelay(600, function() utils.playSound("clap") composer.gotoScene( "scenes.gameOver", {effect="slideLeft", time=300, params={ currentScore=currentScore, onCategory=onCategory, numQuestions="10", }}) end, 1) end -- Easily add to our score, also controls the +1 animation updateScore = function(amount) if amount ~= nil then currentScore = currentScore + amount label\_question.text = currentScore .. " / 10" end end -- Update our timer bar. startTimer = function(delay) if timerEnabled == true and timer\_bar ~= nil then if timer\_trans ~= nil then transition.cancel(timer\_trans) timer\_trans = nil end clockTimer = timer.performWithDelay(1000,doCountup,gameTime) -- Reset xScale and set delay local time = timePerQuestion \* 1000 local startDelay = delay timer\_bar.xScale = 1 if startDelay == nil then startDelay = 0 end timer\_trans = transition.to(timer\_bar, {time=time, delay=startDelay, xScale=0, onComplete=function() createQuestion() end}) end end function doCountup() currentTime = currentTime +1 countUpText.text = currentTime if(currentTime == 0) then countUpText.text = currentTime startTimer() end end local physics = require("physics") physics.start() local tur local base = display.newImage ( "images1/base.png" ) base.x = 500 base.y = 1700 local turret = display.newImage ( "images1/AstroGun.png" ) turret.width = 200 turret.height = 400 turret.x = 500 turret.y = 1700 dangle = 0 function range(x1,y1,x2,y2) return math.sqrt((x2-x1)^2 + (y2-y1)^2) end local function turret\_rotation( event ) turret: rotate(-dangle) local a = (event.x or 42)-(base.x or 42) local b = (event.y or 42)-(base.y or 42) dangle = math.deg(math.atan2(b,a)) + 90 --print (dangle) turret: rotate(dangle) if event.phase == "ended" then local blast = display.newImage ( "images1/bluePlanet.png" ) blast.width = 200 blast.height = 200 blast.x = 500 blast.y = 1700 uiGroup: insert(blast) uiGroup: insert(turret) transition.to(blast, { time=range(base.x,base.y,event.x,event.y)\*1 , x = event.x, y = event.y, transition = easing.inOutExpo}) timer.performWithDelay(1000, myclosure, 1) end end -- Create a quesiton, iterates. createQuestion = function() -- Add one timerEnabled = true onQuestion = onQuestion + 1 -- Make sure we have a question and haven't finished. if questions[onQuestion] == nil or onQuestion \> 10 then gameOver() else -- Remove anything old for i=quizGroup.numChildren, 1, -1 do if quizGroup[i] ~= nil then display.remove(quizGroup[i]) quizGroup[i] = nil end end -- Start out timer, add a delay if we are on quesiton 1 local delay = 0 if onQuestion == 1 then delay = 1000 end startTimer(delay) -- Create the question and answers. local numberX = 32 local questionX = 24 local questionW = \_W-(questionX\*2) local q = questions[onQuestion] local image if q.image ~= nil and q.image.file ~= nil then image = display.newImageRect(quizGroup, q.image.file, q.image.width, q.image.height) image.anchorX = 0 image.x = numberX/2 image.y = question\_rect.y+question\_rect.height/2 questionX = image.x + image.width + numberX/2 questionW = \_W-questionX-numberX end local question = display.newText({parent=quizGroup, text=q.question, width=questionW, height=0, font=averageFont, fontSize=175}) question.x = 190 question.y = question\_rect.y+question\_rect.height/2 question.anchorX = 0 question:setFillColor(1) local areaPadding = 5 local topPadding = 16 local answerOffset = 6 local area = answer\_rect.width - (areaPadding\*2.5) local answerHeight = (area-topPadding-(answerOffset\*#q.answers))/#q.answers local textX = numberX + 20 local textW = \_W - textX - 24 local y = answer\_rect.x + areaPadding + topPadding local howManyAnswers = #q.answers -- make sure answers fit screen size if neccessary decrease answerHeight local offset = (\_W - (howManyAnswers \* (answerHeight+answerOffset) - answerOffset)) \* 0.5 for i=1, howManyAnswers do rect = display.newImage(quizGroup, "images1/averageAsteroid.png",offset + (answerHeight+answerOffset)\*(i-1), y+100, answerHeight, answerHeight) rect.anchorX, rect.anchorY = 0, 0 rect.width = 350 rect.height = 300 rect.id = "answer" rect.index = i rect:addEventListener("touch", buttonTouched) rect: addEventListener("touch", turret\_rotation) local label = display.newText({parent=quizGroup, text=i..".", font=averageFont, fontSize=20}) label.x = rect.x - answerOffset \* 0.5 label.y = rect.y + 0.5 \* rect.height label.anchorX = 0 label:setFillColor(0.4) local answer = display.newText({parent=quizGroup, text=q.answers[i], width=textW, height=0, font=averageFont, fontSize=150}) answer.x = rect.x + rect.width \* 0.3 answer.y = rect.y + rect.height \* 0.5 answer.anchorX = 0 answer:setFillColor(1) end end end local pauseGame = function(event) if (timerEnabled==false and pauseScreen.alpha == 0 ) then timerEnabled=true transition.pause( "transTag" ) timer.pause(clockTimer) pauseScreen.alpha = 1 touchEnabled = false elseif (timerEnabled==true) then timerEnabled=false transition.resume( "transTag" ) timer.resume(clockTimer) pauseScreen.alpha = 0 touchEnabled = true end return true end local pauseButtonPress = function( event ) pauseGame() end ----------------------------------------------- -- \*\*\* COMPOSER SCENE EVENT FUNCTIONS \*\*\* ----------------------------------------------- -- Called when the scene's view does not exist: -- Create all your display objects here. function scene:create( event ) -- Setup our groups screenGroup = self.view uiGroup = display.newGroup() quizGroup = display.newGroup() screenGroup:insert(uiGroup) screenGroup:insert(quizGroup) -- Set our categories and questions if event.params then if event.params.category then onCategory = event.params.category category = categories[onCategory] questions = require( category.questions ) colour = category.colour timePerQuestion = category.timePerQuestion if timePerQuestion == nil or timePerQuestion == 0 then timerEnabled = false end end end -- Display our UI options = { title = "Average - Addition", } local navBar = utils.createNavBar(options) uiGroup:insert(navBar) local top = navBar.y + navBar.height local height = \_H - top question\_rect = display.newRect(uiGroup, \_W\*0.5, top, \_W, 400 ) question\_rect.anchorY = 0 question\_rect:setFillColor(colour[1], colour[2], colour[3]) timer\_rect = display.newRect(uiGroup, \_W\*0.5, question\_rect.y+question\_rect.height, \_W, 50 ) timer\_rect.anchorY = 0 timer\_rect:setFillColor(0.7,0.3,1) answer\_rect = display.newRect(uiGroup, \_W\*0.5, timer\_rect.y+timer\_rect.height, \_W, \_H-(timer\_rect.y+timer\_rect.height)) answer\_rect.anchorY = 0 answer\_rect:setFillColor(0.9) -- Now the score/quesiton num/time remaining if timerEnabled == true then timer\_bar = display.newRect(uiGroup, 0, timer\_rect.y+timer\_rect.height-20, \_W, 35) timer\_bar.anchorX = 0 timer\_bar:setFillColor(0.7,1,1) end label\_question = display.newText({parent=uiGroup, text="0 / 10", font=easyFont, fontSize=75}) label\_question.x = \_W\*0.9 label\_question.y = timer\_rect.y+-450 local pauseButton= widget.newButton{ defaultFile = "images1/Buttons/Pause.png", onRelease = pauseButtonPress, } pauseButton.x = 100 pauseButton.y = 90 currentTime = 0 countUpText = display.newText("0",259,50,native.systemFontBold,100) countUpText:setTextColor("#000000") background = display.newImage(uiGroup, "images1/averageBackground.png", display.contentHeight, display.contentWidth) background.anchorX = 0 background.anchorY = 0 background.x, background.y = 0, 0 pauseScreen = display.newImageRect("images1/PauseScreen.png", 1000, 1500) pauseScreen.alpha = 0 pauseScreen.width = 1080 pauseScreen.height = 1800 pauseScreen.x = 540 pauseScreen.y = 1050 pauseButton:addEventListener("touch", pauseButtonPress) pauseScreen:addEventListener("touch", buttonTouched) -- Now make our first question createQuestion() uiGroup:insert(background) uiGroup:insert(navBar) uiGroup:insert(countUpText) uiGroup:insert(pauseButton) uiGroup:insert(timer\_rect) uiGroup:insert(timer\_bar) uiGroup:insert(label\_question) uiGroup:insert(base) uiGroup:insert(turret) 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 (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. composer.removeHidden() -- Now that the scene has appeared, allow touch touchEnabled = true end end 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. touchEnabled = false -- Stop transitions if needed if timer\_trans then transition.cancel(timer\_trans) timer\_trans = nil end if result\_trans then transition.cancel(result\_trans) result\_trans = nil end if delay\_timer ~= nil then timer.cancel(delay\_timer) delay\_timer = nil end elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end 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 -- Then add the listeners for the above functions scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

createQuestion = function() -- Add one timerEnabled = true onQuestion = onQuestion + 1 -- Make sure we have a question and haven't finished. if questions[onQuestion] == nil or onQuestion \> 10 then gameOver() else -- Remove anything old for i=quizGroup.numChildren, 1, -1 do if quizGroup[i] ~= nil then display.remove(quizGroup[i]) quizGroup[i] = nil end end -- Start out timer, add a delay if we are on quesiton 1 local delay = 0 if onQuestion == 1 then delay = 1000 end startTimer(delay) -- Create the question and answers. local numberX = 32 local questionX = 24 local questionW = \_W-(questionX\*2) local q = questions[onQuestion] local image if q.image ~= nil and q.image.file ~= nil then image = display.newImageRect(quizGroup, q.image.file, q.image.width, q.image.height) image.anchorX = 0 image.x = numberX/2 image.y = question\_rect.y+question\_rect.height/2 questionX = image.x + image.width + numberX/2 questionW = \_W-questionX-numberX end local question = display.newText({parent=quizGroup, text=q.question, width=questionW, height=0, font=easyFont, fontSize=175}) question.x = questionX + 300 question.y = question\_rect.y+question\_rect.height/2 question.anchorX = 0 question:setFillColor(1) -- The answers all need to fit in the bottom area, so we make sure they can! local areaPadding = 3 -- Top and bottom padding local topPadding = 160 -- Extra padding for the top for some added space local answerOffset = 40 -- y offset per answer local area = answer\_rect.width - (areaPadding\*2) local answerHeight = (area-topPadding-(answerOffset\*#q.answers))/#q.answers local textX = numberX + 20 local textW = \_W - textX - 24 for i=1, #q.answers do local y = answer\_rect.y + areaPadding + topPadding + ((answerHeight+answerOffset)\*(i-1)) rect = display.newImage(quizGroup,"images1/Buttons/difficultyBtn.png", \_W\*0.5, y, \_W\*0.5, answerHeight) rect.width = 600 rect.height = 280 rect.anchorY = 0 rect.id = "answer" rect.index = i rect:setFillColor(0,0.3,0.7) rect:addEventListener("touch", buttonTouched) local answer = display.newText({parent=quizGroup, text=q.answers[i], width=textW, height=0, font=easyFont, fontSize=150}) answer.x = rect.x + rect.width \* -0.16 answer.y = rect.y + rect.height \* 0.5 answer.anchorX = 0 answer:setFillColor(1) end end end

menu Scene

---- ------------------------------------------------------------------------- local composer = require( "composer" ) local scene = composer.newScene() ----------------------------------------------- --\*\*\* Set up our variables etc \*\*\* ----------------------------------------------- -- Some handy maths vars local \_W = display.contentWidth local \_H = display.contentHeight local mF = math.floor -- Load our utils and main categories local utils = require("helpers.globals") local difficulties = require("helpers.additionDifficulty") local widget = require( "widget" ) -- Display Groups local screenGroup local uiGroup -- Variables local touchEnabled = false -- Functions local buttonTapped ----------------------------------------------- --\*\*\* OTHER FUNCTIONS \*\*\* ----------------------------------------------- -- Button tapped - For scrollView items buttonTapped = function(event) local t = event.target local category = t.category if category ~= nil and touchEnabled == true then utils.playSound("select") composer.gotoScene(difficulties[category].sceneName, {effect="slideLeft", time=300, params={category=category}}) end return true end local function animate( event ) gear.rotation = gear.rotation + 1 end local function onbackBtnRelease() composer.gotoScene( "scenes.operationMenu", "fade", 250) local btnClick = audio.loadStream("sound/gameClick.wav", {channel = soundAudioChannel}) audio.play(btnClick) return true end ----------------------------------------------- -- \*\*\* COMPOSER SCENE EVENT FUNCTIONS \*\*\* ----------------------------------------------- -- Called when the scene's view does not exist: -- Create all your display objects here. function scene:create( event ) -- Setup our groups screenGroup = self.view uiGroup = display.newGroup() screenGroup:insert(uiGroup) -- Display our UI -- The top bar that is made in a utility function local options = { title = "Select Difficulty", backScene = nil, -- Set this to a scene name if you want a back button } local navBar = utils.createNavBar(options) local top = navBar.y + navBar.height local height = \_H - top -- Now our categories. These are just basic rectangles right now, but can be easily -- changed to an newImageRect if needed. local categoryHeight = 250 local categoryOffset =150 background = display.newImage("images1/mainBackground.png", display.contentHeight, display.contentWidth) background.anchorX = 0 background.anchorY = 0 background.x, background.y = 0, 0 uiGroup:insert(background) uiGroup:insert(navBar) gear = display.newImage( "images1/bluePlanet.png" ) gear.width = 1500 gear.height = 1500 gear.x = 540 gear.y = 1700 uiGroup:insert(gear) Runtime:addEventListener( "enterFrame", animate ); backBtn = widget.newButton{ defaultFile="images1/Buttons/BackButton2.png", overFile="images1/Buttons/BackButton.png", width=120, height=120, onRelease = onbackBtnRelease -- event listener function } backBtn.x = 100 backBtn.y = 85 uiGroup:insert(backBtn) for i=1, #difficulties do local colour = difficulties[i].colour local y = (i-0.5)\*(categoryHeight+categoryOffset) + (categoryHeight/1) local textX = 36 local textW = \_W-(textX\*2) - 64 -- an extra 64 for icons local rect = display.newImage("images1/Buttons/difficultyBtn.png", \_W\*0.5, y, \_W, categoryHeight ) rect.width = 700 rect.height = 250 rect.category = i rect:addEventListener("tap", buttonTapped) uiGroup:insert(rect) local arrow = display.newSprite(utils.uiSheet, {frames={utils.uiSheetInfo:getFrameIndex("arrow")}}) arrow.x = arrow.width/2 + 4 arrow.y = rect.y uiGroup:insert(arrow) local mainFont = "gomarice\_kaiju\_monster\_0.ttf" local title = display.newText({text=difficulties[i].title, x=textX, y=rect.y-6, width=textW, height=0, font=mainFont, fontSize=100}) title.anchorX = 0 title:setFillColor(1) uiGroup:insert(title) if difficulties[i].icon and difficulties[i].icon ~= "" then local icon = display.newSprite(utils.uiSheet, {frames={utils.uiSheetInfo:getFrameIndex(difficulties[i].icon)}}) icon.x = \_W - 40 icon.y = rect.y uiGroup:insert(icon) end end 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 (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. composer.removeHidden() -- Now that the scene has appeared, allow dragging etc touchEnabled = true end end 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. touchEnabled = false elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end function scene:destroy( event ) local sceneGroup = self.view Runtime:removeEventListener("enterFrame", animate); -- 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 -- Then add the listeners for the above functions scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Gameover Scene.

local composer = require( "composer" ) local scene = composer.newScene() local currScene = composer.getSceneName( "scenes.mainMenu" ) local \_W = display.contentWidth local \_H = display.contentHeight local mF = math.floor local utils = require("helpers.globals") local categories = require("helpers.operations") local widget = require "widget" -- Display Groups local screenGroup local uiGroup -- Variables local touchEnabled = false local category = {} local onCategory = 0 local currentScore = 0 local oneStar = 2 local twoStar = 5 local threeStar = 10 local highscore = 0 local numQuestions = 0 local colour = {} -- Functions local buttonTouched ----------------------------------------------- --\*\*\* OTHER FUNCTIONS \*\*\* ----------------------------------------------- -- Button touched buttonTouched = function(event) local t = event.target local id = t.id if event.phase == "began" and touchEnabled == true then display.getCurrentStage():setFocus( t ) t.isFocus = true t.alpha = 0.6 elseif t.isFocus then if event.phase == "ended" then display.getCurrentStage():setFocus( nil ) t.isFocus = false t.alpha = 1 local b = t.contentBounds if event.x \>= b.xMin and event.x \<= b.xMax and event.y \>= b.yMin and event.y \<= b.yMax then utils.playSound("select") if id == "home" then composer.removeScene("helpers.questions.easy.easyAddition") composer.removeScene("helpers.questions.easy.easySubtraction") composer.removeScene("helpers.questions.easy.easyMultiplication") composer.removeScene("helpers.questions.easy.easyDivision") composer.removeScene("helpers.questions.average.averageAddition") composer.removeScene("helpers.questions.average.averageSubtraction") composer.removeScene("helpers.questions.average.averageMultiplication") composer.removeScene("helpers.questions.average.averageDivision") composer.removeScene("helpers.questions.difficult.averageAddition") composer.removeScene("helpers.questions.difficult.averageSubtraction") composer.removeScene("helpers.questions.difficult.averageMultiplication") composer.removeScene("helpers.questions.difficult.averageDivision") composer.gotoScene( "scenes.mainMenu", {effect="slideRight", time=300}) end end end end return true end local function stopTouches( event ) return true end local function onOperationBtnRelease() composer.removeScene("helpers.questions.easy.easyAddition") composer.removeScene("helpers.questions.easy.easySubtraction") composer.removeScene("helpers.questions.easy.easyMultiplication") composer.removeScene("helpers.questions.easy.easyDivision") composer.removeScene("helpers.questions.average.averageAddition") composer.removeScene("helpers.questions.average.averageSubtraction") composer.removeScene("helpers.questions.average.averageMultiplication") composer.removeScene("helpers.questions.average.averageDivision") composer.removeScene("helpers.questions.difficult.averageAddition") composer.removeScene("helpers.questions.difficult.averageSubtraction") composer.removeScene("helpers.questions.difficult.averageMultiplication") composer.removeScene("helpers.questions.difficult.averageDivision") composer.gotoScene( "scenes.operationMenu", "fade", 250) local btnClick = audio.loadStream("sound/gameClick.wav", {channel = soundAudioChannel}) audio.play(btnClick) return true end -- after you create your background object. ----------------------------------------------- -- \*\*\* COMPOSER SCENE EVENT FUNCTIONS \*\*\* ----------------------------------------------- -- Called when the scene's view does not exist: -- Create all your display objects here. function scene:create( event ) -- Setup our groups screenGroup = self.view uiGroup = display.newGroup() screenGroup:insert(uiGroup) -- Set our category, score etc if event.params then if event.params.onCategory then onCategory = event.params.onCategory category = categories[onCategory] colour = category.colour end if event.params.currentScore then currentScore = event.params.currentScore end if event.params.currentTime then currentScore = event.params.currentTime end if event.params.numQuestions then numQuestions = event.params.numQuestions end end -- Display our UI -- The top bar that is made in a utility function local options = { title = options.title, } local navBar = utils.createNavBar(options) uiGroup:insert(navBar) -- Figure out if we need to save this currentScore as our highscore --local highscores = utils.loadFromJson("category\_scores") --if highscores[onCategory] ~= nil then --highscore = highscores[onCategory] --else --highscores[onCategory] = currentScore --end --if currentScore \> highscore then --highscore = currentScore --end --utils.saveToJson(highscores, "category\_scores") -- Now show our bg, score etc local background = display.newImage(uiGroup,"images1/GameOver.png", display.contentHeight, display.contentWidth) background.anchorX = 0 background.anchorY = 0 background.x, background.y = 0, 0 navBar:toFront() background:addEventListener( "touch", stopTouches ) background:addEventListener( "tap", stopTouches ) local score\_label\_1 = display.newText({parent=uiGroup, text="You scored", font=native.systemFont, fontSize=75, align="center"}) score\_label\_1.x = 540 score\_label\_1.y = 300 score\_label\_1:setFillColor(1) local score\_label\_2 = display.newText({parent=uiGroup, text=currentScore, font=native.systemFontBold, fontSize=128, align="center"}) score\_label\_2.x = score\_label\_1.x score\_label\_2.y = mF(score\_label\_1.y + score\_label\_1.height/2 + score\_label\_2.height/2 + 2) score\_label\_2:setFillColor(0.9,0.9,0) composer.setVariable("finalScore", currentScore) local time\_label = display.newText({parent=uiGroup, text=currentTime, font=native.systemFontBold, fontSize=128, align="center"}) time\_label.x = 540 time\_label.y = 750 time\_label:setFillColor(0.9,0.9,0) composer.setVariable("finalTime", currentTime) local time\_label\_1 = display.newText({parent=uiGroup, text="Time:", font=native.systemFont, fontSize=75, align="center"}) time\_label\_1.x = 350 time\_label\_1.y = 750 time\_label\_1:setFillColor(1) local score\_label\_3 = display.newText({parent=uiGroup, text="out of", font=native.systemFont, fontSize=75, align="center"}) score\_label\_3.x = \_W\*0.5 score\_label\_3.y = score\_label\_2.y + score\_label\_2.height/2 + score\_label\_3.height/2 + 12 score\_label\_3:setFillColor(1) local score\_label\_4 = display.newText({parent=uiGroup, text=numQuestions, font=native.systemFontBold, fontSize=80, align="center"}) score\_label\_4.x = score\_label\_1.x score\_label\_4.y = mF(score\_label\_3.y + score\_label\_3.height/2 + score\_label\_4.height/2 + 2) score\_label\_4:setFillColor(1) local noStar = display.newImage(uiGroup, "images1/NoRewardStar.png",display.contentHeight, display.contentWidth) noStar.x = 540 noStar.y = 1100 if currentScore == threeStar then local threeStars = display.newImage(uiGroup, "images1/3RewardStar.png", display.contentHeight, display.contentWidth ) threeStars.x = 540 threeStars.y = 1150 threeStars.alpha = 0 threeStars:scale(4, 4) transition.to( threeStars, {time=1500, alpha=1, xScale=1, yScale=1 } ) elseif currentScore \>= twoStar then local twoStars = display.newImage(uiGroup, "images1/2RewardStar.png" , display.contentHeight, display.contentWidth) twoStars.x = 415 twoStars.y = 1100 twoStars.alpha = 0 twoStars:scale(4, 4) transition.to( twoStars, {time=1500, alpha=1, xScale=1, yScale=1 } ) elseif currentScore \>= oneStar then local oneStars = display.newImage(uiGroup, "images1/1RewardStar.png", display.contentHeight, display.contentWidth ) oneStars.x = 280 oneStars.y = 1200 oneStars.alpha = 0 oneStars:scale(4, 4) transition.to( oneStars, {time=1500, alpha=1, xScale=1, yScale=1 } ) else local noStars = display.newImage(uiGroup, "images1/NoRewardStar.png", display.contentHeight, display.contentWidth) noStars.x = 540 noStars.y = 1100 end homeBtn = widget.newButton{ defaultFile= "images1/Buttons/HomeBtn.png", overFile= "images1/Buttons/HomeBtn.png", width=350, height=250, } homeBtn.id = "home" homeBtn.x = 300 homeBtn.y = 1700 uiGroup:insert(homeBtn) homeBtn:addEventListener("touch", buttonTouched) operationBtn = widget.newButton{ defaultFile= "images1/Buttons/OperatorBtn.png", overFile= "images1/Buttons/OperatorBtn.png", width=350, height=250, onRelease = onOperationBtnRelease } operationBtn.x = 800 operationBtn.y = 1700 uiGroup:insert(operationBtn) 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 (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. composer.removeHidden() -- Now that the scene has appeared, allow dragging etc touchEnabled = true end end 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. touchEnabled = false elseif ( phase == "did" ) then -- Called immediately after scene goes off screen. end end 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 -- Then add the listeners for the above functions scene:addEventListener( "create", scene ) scene:addEventListener( "show", scene ) scene:addEventListener( "hide", scene ) scene:addEventListener( "destroy", scene ) return scene

Hard to say exactly without seeing more code.  Are you using composer?  Is menu a composer-overlay or a composer-scene or just a display-object-panel kind of thing?  My guess is startTimer might be getting called double, then triple on the return from menu. Just a guess.  Could you post more of your code? 

Bob

Make sure that  you call composer.goToScene function only once in menu scene. I have similar issue when I have used event listener to go next scene. 

Sounds like you’ve got a function running on a timer that modifies the count  -AND- you are re-starting that function.

You need to cancel the endlessly running timer you start when you leave the scene OR only start it once.

Following up on @roaminggamer’s comment:  every time you go back to the scene, you’re probably starting another timer; if you print the value after each update, I bet you’ll see the 1-2-3- sequence every time on the console; but they’re happening so fast that the on-screen text box looks like it is jumping 2-4-6- or 3-6-9-.  Save the clockTimer value somewhere; if not-nil then cancel-and-restart. 

My menu is a composer scene

Okay, i try to save the timer.cancel above the createQuestion() it works but the timer delays.

How? Shall i remove return true of the function?

Here’s my createQuestion() function bob.

KilikSky,

I see you added that createQuestion function to your original post.  What I would like to see is ‘all’ the menu scene code and all the code for this scene where the startTimer code is.  

Are you able to post the COMPLETE CODE for ‘menu scene’ and this ‘main action scene’ ???  

It still appears that - the startTimer - seems to run multiple times, each time you leave scene to menu scene and return (this is based on what you stated in your original comments).  There is actually a couple of ways that could be happening.   The scene:create  scene:show and scene:hide would be important sections to look at in both menu scene and this scene.

Maybe one of the more experienced developers can see for sure from this last bit of code you posted, what ‘exactly’ is causing the issue.  But it appears we all think it is that startTimer running multiple times each time you return from menu scene.  As roaming gamer noted be sure to either cancel the timer when you leave the scene or maybe just start it once.

But I would need to see the 2 scenes complete code for me to pinpoint the exact line of code where this problem is created.

I hope this helps.

Bob

Check my menu scene i already edited it… 

Great. How about the complete code for the scene that handles the questions and has the startTimer function in it

Then I can see where and how you have the variable ‘currentTime’ declared, and if for some reason if you have it global, as well as what you have in that scene:show … for that scene.  

You can also put a print statement inside the ‘startTimer’ function and then check your console output to see if indeed that is called more then once when you return to that scene from menu scene.  

Still guessing, but it is also possible that you have the ‘currentTime’ as global and it is not getting reset when you return to this scene…  There also could be somewhere in this scene where you are incrementing ‘currentTime’ in some manner besides just in the ‘doCountUp’ function,.

But, I do not see in your sample code, enough code, to know about that variable.

my startTimer is not called once when i return to the menu scene… my currentTime is global i save the timer on GameOver scene. See my code at the top the 1st one its a complete code for gameplay scene…

I have not had a lot of time today to look at this, but I have a couple of questions :

  1. what do you mean by this … what do you mean by ‘default’  and what ‘previous one’?

     BTW that is the default startTimer than the previous one

  1. I see in your game scene in the scene:hide you cancel several timers. That is good, but why are you not cancelling the ‘clockTimer’  also?

I ask question #2 because it appears in your game scene you only call(exit to) gameOver scene. 

  1. is gameOver scene actually Does gameOver then call menuScene?  

  2. In any case, if game is over, I am not sure why you would not cancel ‘clockTimer’ when the scene hides.  So last question is, can you search all the files/modules of your code for the variable currentTime (that you have as global) and find everywhere you ‘increment’  ‘currentTime’ value? 

  3. You say you save the timer on GameOver scene … why? and which timer are you taking about?

  4. In the statement below,  did you mean ‘when you return ‘FROM’ menuScene’?  because the startTimer function I was asking about is in the mainScene or game scene… so I am confused by this statement :

my startTimer is not called once when i return to the menu scene

no. 3 was correct after the gameplay it will go to gameOver and has a home button for menu scene. I have a currentTime variable only on my gameOver in which i set it to text variable. CurrentTime is where i save my time on text in game Over. ill, put my gameOver Scene

Could you give me some answer to questions 1 & 2 from my last post.

And to be clear, the only scenes (or modules) that the ‘global’ ‘currentTime’ variable is in are the mainScene and the gameOverScene, correct?

I am assuming that it is the ‘countUpText’ that you are reporting is the value that you are having the issue with… I mean that is the variable that is showing the  1,2,3,4  then 2,4,6,8   then  3,6,9,12  you mention in your original post, is that correct?

you might look at these 2 lines of code: (in gameOver scene)

Look at lines 133 and 137.  Do you mean to assign ‘currentScore’ on both of those?  I would ‘guess’ maybe

on line 137 you would be wanting to assign ‘currentTime’ not ‘currentScore’.  I am just guessing. 

I am not saying this is the cause of the problem, just something I noticed.

Hard to say exactly without seeing more code.  Are you using composer?  Is menu a composer-overlay or a composer-scene or just a display-object-panel kind of thing?  My guess is startTimer might be getting called double, then triple on the return from menu. Just a guess.  Could you post more of your code? 

Bob

Make sure that  you call composer.goToScene function only once in menu scene. I have similar issue when I have used event listener to go next scene. 

Sounds like you’ve got a function running on a timer that modifies the count  -AND- you are re-starting that function.

You need to cancel the endlessly running timer you start when you leave the scene OR only start it once.