Timer Value only decreases once.G
Posted: Sat Feb 03, 2018 12:37 pm
Good Day everyone.
I'm currently going back to Love2D after a long hiatus and am finally working on a small project. I apologize if the problem I'm going tor report on is easy for some. I've just been racking my head on this for the past few hours now and I can't seem to see what I'm doing wrong. (Also, I apologize of the formatting of the code is really wonky).
I have a function that is suppose to change the color of a text every time the timer runs out. The problem is that the value of the timer only goes down once instead of going down continuously since it's part of the code being called in love.update. (For example, if the timer value is 10, instead of going [10,9,8...1,0 end], it's just [10, 9 end])
Arguably, I could just put all the color changing code in the titleUpdate function so that it runs (which I did when I was thinking about the code) but I plan to re-use the color changing code at other parts of the UI.
I would appreciate any help given on this.
Thanks in advance.
I'm currently going back to Love2D after a long hiatus and am finally working on a small project. I apologize if the problem I'm going tor report on is easy for some. I've just been racking my head on this for the past few hours now and I can't seem to see what I'm doing wrong. (Also, I apologize of the formatting of the code is really wonky).
I have a function that is suppose to change the color of a text every time the timer runs out. The problem is that the value of the timer only goes down once instead of going down continuously since it's part of the code being called in love.update. (For example, if the timer value is 10, instead of going [10,9,8...1,0 end], it's just [10, 9 end])
Code: Select all
-- colors.lua
local colors = {}
colors[1] = {255, 255, 255} -- White
colors[2] = {255, 0, 0} -- Red
colors[3] = {0, 255, 0} -- Green
colors[4] = {0, 0, 255} -- Blue
-- In charge of changing color instantly after a certain amount of time has passed.
function colors.instChange(maxTime, curTime, val, maxVal, dt)
curTime = maxTime
curTime = curTime - 1
if curTime <= 0 then
if val == maxVal then
val = 1
curTime = maxTime
else
val = val + 1
curTime = maxTime
end
end
end
return colors
Code: Select all
local titleItems = {'Play', 'How', 'Exit'}
local titleSelected = 0
local color = require('src.bin.color')
local colorTitleMaxTime = 5
local colorTitleCurTime = colorTitleMaxTime
local colorTitleVal = 1
-- The parts below this are being called in the (non-library) gamestate handler I'm using.
function titleUpdate(dt)
-- Change the color of the game title.
color.instChange(colorTitleMaxTime, colorTitleCurTime, 1, 4, dt)
end
function titleDraw()
love.graphics.setNewFont(24)
love.graphics.setColor(color[colorTitleVal])
-- Prints the game Title
love.graphics.print('Mini Proj', 0, 0)
-- Prints all the Title Items.
love.graphics.setColor(color[1])
for i=#titleItems,1,-1 do
local ti = titleItems[i]
love.graphics.print(ti, 50, 30*i)
end
-- Prints the Title Item Selector.
if titleSelected ~= 0 then
love.graphics.print('>>', 10, 30*titleSelected)
end
end
function titleKeyPressed(key)
-- For main menu controls. Up and Down to select, Enter/Return to choose.
-- 1 = Play
-- 2 = How
-- 3 = Exit
-- On startup, no item is selected. This will fix it.
if titleSelected == 0 then
if key == 'down' or key == 'up' then
titleSelected = 1
end
elseif titleSelected == 1 then
if key == 'up' then
titleSelected = 3
elseif key == 'down' then
titleSelected = 2
end
elseif titleSelected == 2 then
if key == 'up' then
titleSelected = 1
elseif key == 'down' then
titleSelected = 3
end
elseif titleSelected == 3 then
if key == 'up' then
titleSelected = 2
elseif key == 'down' then
titleSelected = 1
end
if key == 'return' then
love.event.quit()
end
end
end
I would appreciate any help given on this.
Thanks in advance.