Help with adding onto timer iterations.

Hello Corona Labs community,

I’m currently making a game that has two buttons which are supposed to do simple things.

The first button, adventButton, starts a timer, with a preset iteration, that loops through a function that adds to the players exp and gold.

The second button, timeAdd, is supposed to add onto the timers iteration in order to extend the loop.
While the button does what I was expecting it to do, add to a local integer that is used as the iteration value, the timer doesn’t update to the current value.
 

xsVcUL9.png

I tried fixing the problem by making the timer a global while changing the iteration in the addTime event. (Basically just copying the timer code line and putting a “+ 3” at the end)

local adventInProgress = false local adventTime = 0 -- Default iteration Amount local adventTimeAdd = 1 -- Amount added to adventTime ------------- Function for adventButton ------------------ local function adventEvent( event ) if(("ended" == event.phase) and (adventInProgress == false)) then adventTime = 10 local function updateEvent( event ) local count = event.count \_G.goldTotal = \_G.goldTotal + 1 -- Updates player Gold \_G.expTotal = \_G.expTotal + 1 -- Updates player EXP adventTime = adventTime - 1 --Updates Loops Remaining -- Print code to make sure values are being handled properly -- print("Gold: ", \_G.goldTotal, " Exp: ", \_G.expTotal, "Loop #: ", count ) if adventTime == 0 then adventInProgress = false -- Allows button to recieve touch presses print("adventInProgress has been set to false | Timer has stopped") adventTime = 80 end end local gameTimer = timer.performWithDelay(3000, updateEvent, adventTime) adventInProgress = true print("adventEvent has been executed") end end ------------- Function for timeAdd button ------------------ local function addTime( event ) if("ended" == event.phase) then adventTime = adventTime + 3 timeDisplay.text = string.format("Distance: %d", adventTime) print("adventTime increased to ", adventTime) end end

Thank you for taking your time to read this.

P.S. Sorry if the post format is incorrect or awkward, was trying to follow the post guidelines in the pinned post on Newbie Questions.

Hello again,

Found a solution to my problem.
All I had to do was make the timer iteration 0 and then put timer,cancel event within the event so when my adventTime equals zero the timer would stop on its own.
Sorry if you wasted your time reading my post and I apologize for it. Had I put a little more time into researching I could have found a solution before thinking about posting here.

Thank you.

(Fixed code in-case anyone wants to view it)

local function adventEvent( event ) if(("ended" == event.phase) and (adventInProgress == false)) then adventTime = 10 local function updateEvent( event ) local count = event.count \_G.goldTotal = \_G.goldTotal + 1 -- Updates player Gold \_G.expTotal = \_G.expTotal + 1 -- Updates player EXP adventTime = adventTime - 1 --Updates Loops Remaining -- Print code to make sure values are being handled properly -- print("Gold: ", \_G.goldTotal, " Exp: ", \_G.expTotal, "Loop #: ", count ) if adventTime == 0 then adventInProgress = false -- Allows button to recieve touch presses print("adventInProgress has been set to false | Timer has stopped") timer.cancel(event.source) -- Stops timer. end end local gameTimer = timer.performWithDelay(100, updateEvent, 0) adventInProgress = true print("adventEvent has been executed") end end

Hello again,

Found a solution to my problem.
All I had to do was make the timer iteration 0 and then put timer,cancel event within the event so when my adventTime equals zero the timer would stop on its own.
Sorry if you wasted your time reading my post and I apologize for it. Had I put a little more time into researching I could have found a solution before thinking about posting here.

Thank you.

(Fixed code in-case anyone wants to view it)

local function adventEvent( event ) if(("ended" == event.phase) and (adventInProgress == false)) then adventTime = 10 local function updateEvent( event ) local count = event.count \_G.goldTotal = \_G.goldTotal + 1 -- Updates player Gold \_G.expTotal = \_G.expTotal + 1 -- Updates player EXP adventTime = adventTime - 1 --Updates Loops Remaining -- Print code to make sure values are being handled properly -- print("Gold: ", \_G.goldTotal, " Exp: ", \_G.expTotal, "Loop #: ", count ) if adventTime == 0 then adventInProgress = false -- Allows button to recieve touch presses print("adventInProgress has been set to false | Timer has stopped") timer.cancel(event.source) -- Stops timer. end end local gameTimer = timer.performWithDelay(100, updateEvent, 0) adventInProgress = true print("adventEvent has been executed") end end