Adding more time to timer

Im trying to add more time to a timer when a score is reached but when the time is added it will only count down so far and not back to 0.  What am I missing? Also how can I tell it to add more time ever time an addition score of 15 is reached?  Here is what I have now…

[lua]local timeLimit = 20
timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 100)
timeLeft.x = display.contentCenterX
timeLeft.y = display.contentCenterY
timeLeft:setTextColor(0,0,0)

local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
storyboard.gotoScene(“restart”)
end
end
timer.performWithDelay(1000,timerDown,timeLimit)

function shoot(event)
if event.phase == “began” then
bullsEye.x = math.random(0, display.contentWidth - 40)
bullsEye.y = math.random(0, display.contentHeight - 100)
score = score + 1
scoreText.text = score
if score == 15 then
timeLimit = timeLimit + 5
end
end
return true
end[/lua]

Do’t know if it will work but try line 13 - not including  timrlimit but use -1. Wil/mayl create other issue to deal with re timers, but should create a endless call to your timerDown function every 1000.

T.

That worked! Thanks ToeKnee.  Any idea how I can have it add more time every time the score increases 15 pts? right now it only does it once because of the if score == 15 then timeLimit = timeLimit + 5

i want to do it every 15 pts, i.e. 15,30,45, and so on.

I believe in the trial and error learning approach - and your kick yourself when you solve this problem.

I’ll point you in the right direction though.

So your problem is that it only happens ONCE as your score increases as you play the game trying to get a highest score.

So your’ve coded already how to make a variable change value - in this case increase ( you say it does it once) - So why only once??

T.

[lua]if score == 15 then
timeLimit = timeLimit + 5[/lua]

When the score reaches 15 it adds  5 seconds to the time.  What I want to do is have it add 5 seconds to the time every time the score increases 15pts.

"When the ‘’…Variable… "reaches 15 it adds  5 seconds to the time.  What I want to do is have it add 5 seconds to the time every time the "… Variable… “increases 15pts.”

 

So you know why because it only is 15 once - how could you make it 15 more than once??

 

try adding a second variable as a counter.

T.

So I added a second variable called count that increases each time the score increase and then when it gets to 15 the count resets to 0 and starts counting again to 15.  now when the game ends i told it to clear out the time left incase the player has “died”.  the game runs fine the first time restart is clicked, but doesn’t when it runs twice.  the timer goes negative.

the error i get is:

Runtime error

game.lua:61: attempt to perform arithmetic on upvalue ‘timeLimit’ (a nil value)

stack traceback:

game.lua:61: in function ‘_listener’

    ?: in function <?:141>

    ?: in function <?:218>

[lua]local timeLimit = 20
local count = 0
timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 100)
timeLeft.x = display.contentCenterX
timeLeft.y = display.contentCenterY
timeLeft:setFillColor(0,0,0)

local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
storyboard.purgeAll()
storyboard.gotoScene(“restart”)
end
end
timer.performWithDelay(1000,timerDown,-1)

function shoot(event)
if event.phase == “began” then
bullsEye.x = math.random(0, display.contentWidth - 40)
bullsEye.y = math.random(0, display.contentHeight - 100)
score = score + 1
count = count + 1
scoreText.text = score
if count == 15 then
timeLimit = timeLimit + 5
count = 0
end
end
return true
end

function endGame(event)
if event.phase == “began” then
bullsEye.alpha = 0
timeLimit = nil
storyboard.gotoScene(“restart”)
end
return true
end[/lua]

Based on what you posted in your original post - adding a count variable to mirror the score variable - and then using that count variable to add time to a timer should work. There were 2 ways you could do it the simple count way and the complicated math equation of dividing by 15 and checking the answer is a whole number.

Are you saying it does not work.

Another question if you are starting this new project why are you using storyboard as composer is the new and actively supported scene system. To me are you not setting yourself up for more problems as and when you might convert to composer.

The error points to line 61 game.lua - i would put print statements ( i do it at the start and end) in all your functions - that way you can see in the terminal the flow of code and narrow down any problems. ( i.e. if you see print “start function A” and then “end function A” then logically you you could assume function A is not causing any problems at this time.)

T.

Also does this not look better

[lua]local timeLimit = 20

local count = 0

timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 100)

timeLeft.x = display.contentCenterX

timeLeft.y = display.contentCenterY

timeLeft:setFillColor(0,0,0)

local function timerDown()

    timeLimit = timeLimit-1

    timeLeft.text = timeLimit

    if(timeLimit==0)then

        storyboard.purgeAll()

        storyboard.gotoScene(“restart”)

    end

end

timer.performWithDelay(1000,timerDown,-1)

function shoot(event)

    if event.phase == “began” then

        bullsEye.x = math.random(0, display.contentWidth - 40)

        bullsEye.y = math.random(0, display.contentHeight - 100)

        score = score + 1

        count = count + 1

        scoreText.text = score

        if count == 15 then

            timeLimit = timeLimit + 5

            count = 0

        end

    end

    return true

end

function endGame(event)

    if event.phase == “began” then

        bullsEye.alpha = 0

        timeLimit = nil

        storyboard.gotoScene(“restart”)

    end

    return true

end

[/lua]

Do’t know if it will work but try line 13 - not including  timrlimit but use -1. Wil/mayl create other issue to deal with re timers, but should create a endless call to your timerDown function every 1000.

T.

That worked! Thanks ToeKnee.  Any idea how I can have it add more time every time the score increases 15 pts? right now it only does it once because of the if score == 15 then timeLimit = timeLimit + 5

i want to do it every 15 pts, i.e. 15,30,45, and so on.

I believe in the trial and error learning approach - and your kick yourself when you solve this problem.

I’ll point you in the right direction though.

So your problem is that it only happens ONCE as your score increases as you play the game trying to get a highest score.

So your’ve coded already how to make a variable change value - in this case increase ( you say it does it once) - So why only once??

T.

[lua]if score == 15 then
timeLimit = timeLimit + 5[/lua]

When the score reaches 15 it adds  5 seconds to the time.  What I want to do is have it add 5 seconds to the time every time the score increases 15pts.

"When the ‘’…Variable… "reaches 15 it adds  5 seconds to the time.  What I want to do is have it add 5 seconds to the time every time the "… Variable… “increases 15pts.”

 

So you know why because it only is 15 once - how could you make it 15 more than once??

 

try adding a second variable as a counter.

T.

So I added a second variable called count that increases each time the score increase and then when it gets to 15 the count resets to 0 and starts counting again to 15.  now when the game ends i told it to clear out the time left incase the player has “died”.  the game runs fine the first time restart is clicked, but doesn’t when it runs twice.  the timer goes negative.

the error i get is:

Runtime error

game.lua:61: attempt to perform arithmetic on upvalue ‘timeLimit’ (a nil value)

stack traceback:

game.lua:61: in function ‘_listener’

    ?: in function <?:141>

    ?: in function <?:218>

[lua]local timeLimit = 20
local count = 0
timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 100)
timeLeft.x = display.contentCenterX
timeLeft.y = display.contentCenterY
timeLeft:setFillColor(0,0,0)

local function timerDown()
timeLimit = timeLimit-1
timeLeft.text = timeLimit
if(timeLimit==0)then
storyboard.purgeAll()
storyboard.gotoScene(“restart”)
end
end
timer.performWithDelay(1000,timerDown,-1)

function shoot(event)
if event.phase == “began” then
bullsEye.x = math.random(0, display.contentWidth - 40)
bullsEye.y = math.random(0, display.contentHeight - 100)
score = score + 1
count = count + 1
scoreText.text = score
if count == 15 then
timeLimit = timeLimit + 5
count = 0
end
end
return true
end

function endGame(event)
if event.phase == “began” then
bullsEye.alpha = 0
timeLimit = nil
storyboard.gotoScene(“restart”)
end
return true
end[/lua]

Based on what you posted in your original post - adding a count variable to mirror the score variable - and then using that count variable to add time to a timer should work. There were 2 ways you could do it the simple count way and the complicated math equation of dividing by 15 and checking the answer is a whole number.

Are you saying it does not work.

Another question if you are starting this new project why are you using storyboard as composer is the new and actively supported scene system. To me are you not setting yourself up for more problems as and when you might convert to composer.

The error points to line 61 game.lua - i would put print statements ( i do it at the start and end) in all your functions - that way you can see in the terminal the flow of code and narrow down any problems. ( i.e. if you see print “start function A” and then “end function A” then logically you you could assume function A is not causing any problems at this time.)

T.

Also does this not look better

[lua]local timeLimit = 20

local count = 0

timeLeft = display.newText(timeLimit, 160, 20, native.systemFontBold, 100)

timeLeft.x = display.contentCenterX

timeLeft.y = display.contentCenterY

timeLeft:setFillColor(0,0,0)

local function timerDown()

    timeLimit = timeLimit-1

    timeLeft.text = timeLimit

    if(timeLimit==0)then

        storyboard.purgeAll()

        storyboard.gotoScene(“restart”)

    end

end

timer.performWithDelay(1000,timerDown,-1)

function shoot(event)

    if event.phase == “began” then

        bullsEye.x = math.random(0, display.contentWidth - 40)

        bullsEye.y = math.random(0, display.contentHeight - 100)

        score = score + 1

        count = count + 1

        scoreText.text = score

        if count == 15 then

            timeLimit = timeLimit + 5

            count = 0

        end

    end

    return true

end

function endGame(event)

    if event.phase == “began” then

        bullsEye.alpha = 0

        timeLimit = nil

        storyboard.gotoScene(“restart”)

    end

    return true

end

[/lua]