Timer help (Game Logic Help!)

Hello everybody! 

I have this code which activates a “Mole” from appearing and disappearing in different holes. I would like to know if there is a way to make the “Mole” do the same function BUT appear and disappear faster as time progresses. I would really appreciate the replies, thanks!

Code: 

– Timer

function startTimer()          timerSource = timer.performWithDelay(1000, showMole, 0) end  

– Show Mole

function showMole(e)    if(currentMoles == totalMoles) then         alert()     else         lastBush.isVisible = false         local randomHole = math.random(1, 11)         lastMole = bushs[randomHole]         lastMole:setReferencePoint(display.BottomCenterReferencePoint)         lastMole.yScale = 0.1         lastMole.isVisible = true                  Runtime:addEventListener('enterFrame', popOut)              currentMoles = currentMoles + 1     end end  

You could try using a variable as the timer limit and increase it with each successful function iteration. Something like this:

local timeLmt = 1000
timerSource = timer.performWithDelay(timeLmt, showMole, 0)

function showMole()
timeLmt= timeLmt+50
end

okay lets say i want the “mole” to popout faster everytime it gets hit 20 times, how would i use the timeLmt for that? Thanks a lot i appreciate it!

If it were me, I’d use a counter that increases on each hit, once the counter gets to 20 I’d increase the speed, then reset the counter and start the process again. Something like this: 

local timeLmt = 1000 local showMole timerSource = timer.performWithDelay(timeLmt, showMole, 0) function showMole() if moveObj == isHit then hitCount = hitCount + 1 if hitCount == 20 then hitCount = 0 timeLmt= timeLmt+50 end end end

so how would i incorporate the code you gave me into the previous one i wrote at the top? because i did exactly what you said but their speeds stay the same

hey @panc software , i have incorporated the code you have given me and it seems to say this on the Corona Runtime error:

attempt to perform arithmetic on upvalue ‘hitCount’ ( a table value) 

yea, you need to declare your “hit” variable ahead of time if that is the path you’re going to take. I didn’t post plug-and-play code as in not familiar with your project.

You can stick a “hitCount = 0” at the top of your code and it should work then.

Thanks a lot problem solved!

is the 0 in the “hitCount = 0” the number needed to increase the hits needed to popout faster or is it “0” by default? thanks a lot , i appreciate it!

the 0 there is just the “start” number. The  if hitCount == 20 then  is what tells the code to “go faster”, when the hitCount number reaches 20.

in the 
“timeLmt= timeLmt+50” part of the code, the number “50” represents the milliseconds increased in the timer of the objects, right?

so would the higher the number increase the speed or the lower the number is what increases the speed, thanks! Much appreciated! 

A different option might be to look at the “enterFrame” event which includes a field for event.time (in milliseconds).  As the player progresses you could just adjust the rate at which you trigger other events (like creation/removal of moles):

trigger\_diff = 1000 last\_trigger = 0 function process\_each\_frame( evt ) local current\_time = evt.time if( (current\_time-last\_trigger) \>= trigger\_diff ) then -- trigger a new downstream event (create/remove a mole) last\_trigger = current\_time end -- other logic here to alter the speed of the game if( user\_does\_bad\_thing ) then trigger\_diff = trigger\_diff - 100 -- trigger future events faster elseif( user\_does\_good\_thing ) then trigger\_diff = trigger\_diff + 100 -- trigger future events slower end end Runtime:addEventListener( "enterFrame", process\_each\_frame )

helo everyone i have a same problem…

but for me in my game i have a powerups then if i click it i want to add “5” seconds in my time? 

–this my code:


timeLimit = 10

timerdown = display.newText(timeLimit, 280, -37, “Arial”, 20)

timer1=timer.performWithDelay(1000,timerDown,timeLimit)

function plus5sec()

timeLimit = timeLimit + 5

end


but ders something wrong when i click plus5sec()

because the timerDown will stop when it comes into 5 seconds…

and i dont know why…

any help …

tnx advance & Merry Christmas  

@panc software

I appreciate your help on my game, but now unfortunately the code does not work at all within my “showMole function” Once i added the hitCount = 0 variable, the Corona runtime error did not appear but the code does nothing at all now. Here is what I have so far:

Here is my entire code if you kindly would take a better look at it, I am completely stummped!

local score -- Mole local moleGroup = display.newGroup() local moles = {} local lastMole = {} -- Sound local hit = audio.loadSound('hit.wav') -- Variables local timerSource local currentMoles = 0 local molesHit = 0 local timeLimit = 1000 local hitCount = 0 -- Functions local Main = {} local startButtonListeners = {} local showCredits = {} local hideCredits = {} local showGameView = {} local prepareMoles = {} local startTimer = {} local showMole = {} local popOut = {} local moleHit = {} local alert = {} local lifeIcons = {} local lives = 3 local maxLives = 3 local i for i = 1, maxLives do      lifeIcons[i] = display.newImage("lifeicon.png")     lifeIcons[i].x = 57 + (lifeIcons[i].contentWidth \* (i - 1))     lifeIcons[i].y = 23 end function prepareMoles()     b1 = display.newImage('mole4.png', 12, 123)     b1.type = "mole"     b2 = display.newImage('mole1.png', 50, 32)     b2.type = "mole"     b3 = display.newImage('mole2.png', 360, 34)     b3.type = "mole"     b4 = display.newImage('mole3.png', 210, 7)     b4.type = "mole"     b5 = display.newImage('mole4.png', 193, 168)     b5.type = "mole"     b6 = display.newImage('mole5.png', 388, 126)     b6.type = "mole"     b7 = display.newImage('mole6.png', 302, 10)        b7.type = "mole"     b8 = display.newImage('mole.png', 200, 15)     b8.type = "mole"     b9 = display.newImage('mole.png', 200, 15)     b9.type = "mole"          moles = display.newGroup(b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11)          for i = 1, 11 do         moles[i]:addEventListener('tap', moleHit)         moles[i].isVisible = false     end          startTimer() end function startTimer()         timerSource = timer.performWithDelay(timeLimit, showMole, 0) end local moveObject = {} function showMole(e)         lastMole.isVisible = false         local randomHole = math.random(1, 11)         lastMole = moles[randomHole]         lastMole:setReferencePoint(display.BottomCenterReferencePoint)         lastMole.yScale = 0.1         lastMole.isVisible = true                  if moveobj == isHit then                 hitCount = hitCount + 1                 if hitCount == 5 then                         hitCount =0                         timeLimit= timeLimit+500                 end         end                  Runtime:addEventListener('enterFrame', popOut) end function popOut(e)     lastMole.yScale = lastMole.yScale + 0.2          if(lastMole.yScale \>=1) then         Runtime:removeEventListener('enterFrame', popOut)     end end function moleHit:tap(e)         audio.play(hit)              lastMole.isVisible = false     local t = e.target     if t.type == "mole" then     molesHit = molesHit + 1     moleHit = display.newText('+1000', 380, 45, native.systemFontBold, 18)     moleHit:setTextColor(255, 0, 0)     transition.to( moleHit, { time=800, alpha=.01 } )     score.text = molesHit      elseif t.type == "mole" then     lifeIcons[lives].isVisible = false     lives = lives - 1      end     if( lives == 0) then     alert() end     end  

That is my main coding for the game to function properly. I really appreciate your help on this project and can’t thank you enough! Keep coding and Merry Christmas !

problem solve… ^^

How did you solve your dilema @juario soul? That also is another issue I was looking into.

hehe i share it to you this is my code… :slight_smile: Merry Christmas 


 

 

timeLimit = 60

timerdown = display.newText(timeLimit, 278, -37, “Arial”, 20)  

  local function timerDown()

   timeLimit = timeLimit-1

   timerdown.text = timeLimit

 end

 

timer1=timer.performWithDelay(1000,timerDown,timeLimit)

 

 

function plus5sec() – if you use this function its gives plus + 5 seconds into the timer…

result=timer.cancel(timer1)

timeLimit = timerdown.text + 5

display.remove(timerdown)

timerdown =nil

timer1=timer.performWithDelay(1000,timerDown,timeLimit)

timerdown = display.newText(timeLimit, 280, -37, “Arial”, 20)

end


 

 

i hope its help 2 u… ^^.

You could try using a variable as the timer limit and increase it with each successful function iteration. Something like this:

local timeLmt = 1000
timerSource = timer.performWithDelay(timeLmt, showMole, 0)

function showMole()
timeLmt= timeLmt+50
end

okay lets say i want the “mole” to popout faster everytime it gets hit 20 times, how would i use the timeLmt for that? Thanks a lot i appreciate it!

If it were me, I’d use a counter that increases on each hit, once the counter gets to 20 I’d increase the speed, then reset the counter and start the process again. Something like this: 

local timeLmt = 1000 local showMole timerSource = timer.performWithDelay(timeLmt, showMole, 0) function showMole() if moveObj == isHit then hitCount = hitCount + 1 if hitCount == 20 then hitCount = 0 timeLmt= timeLmt+50 end end end

so how would i incorporate the code you gave me into the previous one i wrote at the top? because i did exactly what you said but their speeds stay the same